Skip to content

Commit 3f093b4

Browse files
committed
Added addChildCdata method to XmlElement. Added ability to turn off formatting for XmlFiles. Formatter is broken for CDATA sections that contain HTML.
1 parent f6d3a39 commit 3f093b4

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Dependencies
2020
- Extensions:
2121
- CURL
2222
- mongodb client
23+
- libxml (--enable-libxml)
24+
- simplexml
2325
- "Altumo Packages" require mongodb 1.2+
2426
- Classes under the Symfony_1_4 directory depend on Symfony 1.4
2527

source/php/Xml/XmlElement.php

+57-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class XmlElement{
3939
*
4040
* @param string|\SimpleXMLElement $xml_string
4141
* @throws \Exception if SimpleXML is not loaded
42-
* @return XmlElement
42+
* @return \Altumo\Xml\XmlElement
4343
*/
4444
public function __construct( $xml_string = null ){
4545

@@ -159,6 +159,7 @@ public function queryWithXpath( $xpath_query, $return_type = self::RETURN_TYPE_S
159159
* @param string $filename
160160
* @throws \Exception if file does not exist
161161
* @throws \Exception if contents are not valid XML
162+
* @return \Altumo\Xml\XmlElement
162163
*/
163164
public function loadFromFile( $filename ){
164165

@@ -169,6 +170,8 @@ public function loadFromFile( $filename ){
169170
$contents = file_get_contents($filename);
170171
$this->setContentsByString($contents);
171172

173+
return $this;
174+
172175
}
173176

174177

@@ -177,6 +180,7 @@ public function loadFromFile( $filename ){
177180
*
178181
* @param string $xml_string
179182
* @throws \Exception on parse error
183+
* @return \Altumo\Xml\XmlElement
180184
*/
181185
public function setContentsByString( $xml_contents ){
182186

@@ -203,6 +207,8 @@ public function setContentsByString( $xml_contents ){
203207
if( !$this->isValid() ){
204208
throw new \Exception('XML Failed to parse.');
205209
}
210+
211+
return $this;
206212

207213
}
208214

@@ -211,6 +217,7 @@ public function setContentsByString( $xml_contents ){
211217
* Creates a XmlElement
212218
*
213219
* @param string $xml_string
220+
* @return \Altumo\Xml\XmlElement
214221
*/
215222
public function setContentsBySimpleXMLElement( $xml_contents ){
216223

@@ -221,7 +228,8 @@ public function setContentsBySimpleXMLElement( $xml_contents ){
221228

222229
$this->xml_element = $xml_contents;
223230
$this->setLoaded(true);
224-
$this->setValid(true);
231+
$this->setValid(true);
232+
return $this;
225233

226234
}
227235

@@ -384,7 +392,7 @@ public function getXmlAsString( $pretty = false, $include_xml_declaration = true
384392
* @param string $name
385393
* @param string $value
386394
* @param string $namespace
387-
* @return XmlElement
395+
* @return \Altumo\Xml\XmlElement
388396
*/
389397
public function addAttribute( $name, $value, $namespace = null ){
390398

@@ -401,17 +409,62 @@ public function addAttribute( $name, $value, $namespace = null ){
401409
* @param string $name
402410
* @param string $value
403411
* @param string $namespace
404-
* @return XmlElement
412+
* @return \Altumo\Xml\XmlElement
405413
*/
406414
public function addChild( $name, $value = null, $namespace = null ){
407415

408416
$this->assertLoaded();
417+
/*
418+
if( is_string($value) ){
419+
//$value = str_replace( ' ', ' ', $value );
420+
//$value = htmlentities( $value , ENT_COMPAT | ENT_HTML401 );
421+
}*/
409422
$child = $this->getXmlElement()->addChild( $name, $value, $namespace );
423+
410424
return new XmlElement($child);
411425

412426
}
413427

414428

429+
/**
430+
* Add CDATA text in a node
431+
* @param string $cdata_text The CDATA value to add
432+
*
433+
* @author Alexandre FERAUD
434+
* @see http://www.php.net/manual/en/simplexmlelement.addchild.php
435+
*/
436+
protected function addCdata( $cdata_text ){
437+
438+
$node = dom_import_simplexml( $this->getXmlElement() );
439+
$owner_document = $node->ownerDocument;
440+
$node->appendChild( $owner_document->createCDATASection($cdata_text) );
441+
442+
}
443+
444+
445+
/**
446+
* Create a child element with CDATA value.
447+
*
448+
* @param string $name
449+
* //The name of the child element to add.
450+
*
451+
* @param string $cdata_text
452+
* //The CDATA value of the child element.
453+
*
454+
* @author Alexandre FERAUD
455+
* @see http://www.php.net/manual/en/simplexmlelement.addchild.php
456+
*
457+
* @return \Altumo\Xml\XmlElement
458+
*/
459+
public function addChildCdata( $name, $cdata_text, $namespace = null ){
460+
461+
$child = $this->addChild( $name, null, $namespace );
462+
$child->addCdata( $cdata_text );
463+
return $this;
464+
465+
}
466+
467+
415468
/**
416469
* Formats the xml string provided as a pretty output.
417470
*

source/php/Xml/XmlFile.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,16 @@ protected function assertFileOpen(){
317317
* @param boolean $write //whether this method should write
318318
* the contents before closing.
319319
* Defaults to yes.
320+
*
321+
* @param boolean $format_xml //whether the xml will be "pretty"
322+
*
320323
*/
321-
public function closeFile( $write = true ){
324+
public function closeFile( $write = true, $format_xml = true ){
322325

323326
if( $this->isFileOpen() ){
324327

325328
if( !$this->isReadOnly() ){
326-
$this->writeToFile();
329+
$this->writeToFile( $format_xml );
327330
}
328331

329332
fclose( $this->getFilePointer() );
@@ -341,12 +344,13 @@ public function closeFile( $write = true ){
341344
/**
342345
* Saves the contents to the file.
343346
*
347+
* @param boolean $format_xml //whether the xml will be "pretty"
344348
*/
345-
public function writeToFile(){
349+
public function writeToFile( $format_xml = true ){
346350

347351
$this->assertFileOpen();
348352
$this->assertFileWritable();
349-
$file_contents = $this->getXmlRoot()->getXmlAsString(true);
353+
$file_contents = $this->getXmlRoot()->getXmlAsString( $format_xml );
350354
$file_pointer = $this->getFilePointer();
351355
ftruncate( $file_pointer, 0 );
352356
rewind( $file_pointer );

0 commit comments

Comments
 (0)