|
simplexml_load_string
Interprets a string of XML into an object
(PHP 5)
Example 2263. Interpret an XML string<?php The above example will output: SimpleXMLElement Object
At this point, you can go about using Related Examples ( Source code ) » simplexml load string Examples ( Source code ) » Build query string based on form input Examples ( Source code ) » Parse http link from string Examples ( Source code ) » Use simplexml_load_file to load xml file Examples ( Source code ) » Save Upload File to a new directory Examples ( Source code ) » Upload PDF file and rename it Examples ( Source code ) » File Upload Global Variables Examples ( Source code ) » Write string to text file Examples ( Source code ) » See what portion of a string is composed only of a given set of characters Examples ( Source code ) » ucwords() function capitalizes the first letter of each word in a string Examples ( Source code ) » Change String case to lower Examples ( Source code ) » Change String case to Upper Examples ( Source code ) » Trimming Strings Examples ( Source code ) » String compare demo Examples ( Source code ) » String compare Examples ( Source code ) » String compare with if statement Code Examples / Notes » simplexml load stringyoux_free_fr
While needing to add an xml subtree to an existing simplexml object, I noticed that simplexml_load_string fails with strings like <emptynode></emptynode> I needed to use dom instead of simplexml to bypass this problem and work with any kind of xml strings. m dot ament
Warning: The parsing of XML-data will stop when reaching character 0. Please avoid this character in your XML-data. hattori
Theres a problem with the below workaround when serializing fields containing html CDATA. For any other content type then HTML try to modfiy function parseCDATA. Just add these lines before serializing. This is also a workaround for this bug http://bugs.php.net/bug.php?id=42001 <?PHP if(strpos($content, '<![CDATA[')) { function parseCDATA($data) { return htmlentities($data[1]); } $content = preg_replace_callback( '#<!\[CDATA\[(.*)\]\]>#', 'parseCDATA', str_replace("\n", " ", $content) ); } ?> h
seems like simplexml has a line-length restriction - fails if a largeish XML doc with no linebreaks is passed as a string or file. h nbijnens
Please note that not all LIBXML options are supported with the options argument. For instance LIBXML_XINCLUDE does not work. But there is however a work around: <?php $xml = new DOMDocument(); $xml->loadXML ($XMLString); $xml->xinclude(); $xml = simplexml_import_dom($xml); ?> php
It's worth noting that in the example above, $xml->body will actually return an object of type SimpleXMLElement, not a string, e.g. SimpleXMLElement Object ( [0] => this is the text in the body tag ) If you want to get a string out of it you must explicitly cast it using (string) or double quotes, or pass $xml->body (or whatever attribute you want to access) to any function that returns a string, such as urldecode() or trim(). hattori
If you want to serialize and unserialize SimpleXMLElement objects for caching, you need to transform the SimpleXMLElement object into a standard class object before unserializing. This is only if you want to cache converted data, the functionallity of the SimpleXMLElement will not be held. $content = '<SomeXML....' $serialized = str_replace( array('O:16:"SimpleXMLElement":0:{}', 'O:16:"SimpleXMLElement":'), array('s:0:"";', 'O:8:"stdClass":'), serialize(simplexml_load_string($content)) ); $unserialized = unserialize($serialized); paul
If you have PHP > 5.1 and LibXML > 2.6, use this function call to have simplexml convert CDATA into plain text. simplexml_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA); Too bad, so sad with PHP < 5.1. pedro
Attention: simplexml_load_string has a problem with entities other than (<, >, &, " and '). Use numeric character references instead! david somerset
Another option for having simplexml convert CDATA into plain text is to simply type cast the result to string. Here's an example: $xmlRoot = simplexml_load_string('<?xml version="1.0"?> <tvshows> <show> <name>The Simpsons</name> </show> <show> <name><![CDATA[Lois & Clark]]></name> </show> </tvshows>'); $show = array(); foreach($xmlRoot as $val){ $show[] = (string) $val->name; } mindpower
A simple extension that adds a method for retrieving a specific attribute: <?php class simple_xml_extended extends SimpleXMLElement { public function Attribute($name) { foreach($this->Attributes() as $key=>$val) { if($key == $name) return (string)$val; } } } $xml = simplexml_load_string(' <xml> <dog type="poodle" owner="Mrs Smith">Rover</dog> </xml>', 'simple_xml_extended'); echo $xml->dog->Attribute('type'); ?> outputs 'poodle' I prefer to use this technique rather than typecasting attributes. |
Change LanguageSimpleXMLElement->addAttribute() SimpleXMLElement->addChild() SimpleXMLElement->asXML() SimpleXMLElement->attributes() SimpleXMLElement->children() SimpleXMLElement->__construct() SimpleXMLElement->getDocNamespaces() SimpleXMLElement->getName() SimpleXMLElement->getNamespaces() SimpleXMLElement->registerXPathNamespace() SimpleXMLElement->xpath() simplexml_import_dom simplexml_load_file simplexml_load_string |