How to write CDATA using SimpleXmlElement?

Got it! I adapted the code from this great solution (archived version): <?php // http://coffeerings.posterous.com/php-simplexml-and-cdata class SimpleXMLExtended extends SimpleXMLElement { public function addCData( $cdata_text ) { $node = dom_import_simplexml( $this ); $no = $node->ownerDocument; $node->appendChild( $no->createCDATASection( $cdata_text ) ); } } $xmlFile=”config.xml”; // instead of $xml = new SimpleXMLElement( ‘<site/>’ ); $xml = new SimpleXMLExtended( … Read more

Remove a child with a specific attribute, in SimpleXML for PHP

While SimpleXML provides a way to remove XML nodes, its modification capabilities are somewhat limited. One other solution is to resort to using the DOM extension. dom_import_simplexml() will help you with converting your SimpleXMLElement into a DOMElement. Just some example code (tested with PHP 5.2.5): $data=”<data> <seg id=”A1″/> <seg id=”A5″/> <seg id=”A12″/> <seg id=”A29″/> <seg … Read more

How check if a String is a Valid XML with-out Displaying a Warning in PHP

Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards. Simple XML loading string libxml_use_internal_errors(true); $doc = simplexml_load_string($xmlstr); $xml = explode(“\n”, $xmlstr); if (!$doc) { $errors = libxml_get_errors(); foreach ($errors as $error) { echo display_xml_error($error, $xml); } libxml_clear_errors(); }

Error: “Input is not proper UTF-8, indicate encoding !” using PHP’s simplexml_load_string

Your 0xED 0x6E 0x2C 0x20 bytes correspond to “ín, ” in ISO-8859-1, so it looks like your content is in ISO-8859-1, not UTF-8. Tell your data provider about it and ask them to fix it, because if it doesn’t work for you it probably doesn’t work for other people either. Now there are a few … Read more

What’s the difference between PHP’s DOM and SimpleXML extensions?

In a nutshell: SimpleXml is for simple XML and/or simple UseCases limited API to work with nodes (e.g. cannot program to an interface that much) all nodes are of the same kind (element node is the same as attribute node) nodes are magically accessible, e.g. $root->foo->bar[‘attribute’] DOM is for any XML UseCase you might have … Read more

Using SimpleXML to create an XML object from scratch

Sure you can. Eg. <?php $newsXML = new SimpleXMLElement(“<news></news>”); $newsXML->addAttribute(‘newsPagePrefix’, ‘value goes here’); $newsIntro = $newsXML->addChild(‘content’); $newsIntro->addAttribute(‘type’, ‘latest’); Header(‘Content-type: text/xml’); echo $newsXML->asXML(); ?> Output <?xml version=”1.0″?> <news newsPagePrefix=”value goes here”> <content type=”latest”/> </news> Have fun.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)