PHP Load XML document with simplexml_load_string -


i new work xml files. i've been trying load xml document following format object or array: after edit

<?xml version="1.0"?> <xdm:device xmlns:xdm="http://www.example.com/schemas/imaging/con/xdm/1.1/"             xmlns:dd="http://www.example.com/schemas/imaging/con/dictionaries/1.0/"             xmlns:bsi="http://www.example.com/schemas/imaging/con/bsi/2003/08/21"             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"             xmlns:count="http://www.example.com/schemas/imaging/con/counter/2006/01/13"             xmlns:media="http://www.example.com/schemas/imaging/con/media/2006/01/13/"             xmlns:xsi="http://www.example.org/2001/xmlschema-instance"             xmlns:tt="http://www.example.com/schemas/imaging/con/capabilities/1.1/"             xmlns:pwg="http://www.example.com/schemas/imaging/con/pwg/sm/1.0/">     <xdm:information>                 <xdm:component id="system" componenttype="system">             <dd:makeandmodel>samsung laserjet ml220</dd:makeandmodel>             <dd:description>blackand while printer</dd:description>             <dd:productnumber>xb3zzz</dd:productnumber>             <dd:manufacturer>                 <dd:name>samsung</dd:name>             </dd:manufacturer>         </xdm:component>     </xdm:information>     </xdm:device> 

i every entrance instance makeandmodel, description, manufacturer, etc.

i trying load code following code currently, i've tried lot of things:

$xmlstring = file_get_contents('test.xml'); $xml = simplexml_load_string($xmlstring);  var_dump($xml); 

i have tried other simpler xml documents , works fine. document not work, loads empty object instead. file not have title. read in php.net following in 1 of examples:

// file test.xml contains xml document root element // , @ least element /[root]/title.

how can achieve load xml array or object?

edit2:

when use simple xml file example var_dump outputs following:

[root@windows8 csvmail]# php ppp.php  object(simplexmlelement)#1 (1) {   ["movie"]=>   object(simplexmlelement)#2 (5) {     ["title"]=>     string(22) "php: behind parser" 

whit real file if following:

[root@windows8 csvmail]# php ppp.php  object(simplexmlelement)#1 (0) { } [root@windows8 csvmail]#  

the nodes : in them in "xml namespaces". used mix multiple types of data 1 file, if tag names otherwise collide.

at top of file, xmlns:... attributes associate prefixes (which changed whatever generating file) urls (which act permanent identifier particular type of data).

to access them simplexml, use the ->children() method "select" namespace. can rely on prefix not changing, or can store permanent identifiers in variable or constant, , use that, this:

// defining own ways of referring namespaces, regardless of prefix used define('xmlns_hp_xdm_1_1', 'http://www.hp.com/schemas/imaging/con/xdm/1.1/'); define('xmlns_hp_dict_1_0', 'http://www.hp.com/schemas/imaging/con/dictionaries/1.0/');  // load xml in normal way $sx = simplexml_load_string($xml);  // switch first namespace, , @ first component $component = $sx->children(xmlns_hp_xdm_1_1)->information->component;  // attributes no : in technically in no namespace @ all, // have switch null namespace using attributes() $component_id = (string)$component->attributes(null)->id;  // switch second namespace find makeandmodel $make_and_model = (string)$component->children(xmlns_hp_dict_1_0)->makeandmodel;  echo "$component_id : $make_and_model"; 

[live demo]

for how loop on multiple elements, , other basic usage, see the examples in php manual.


Comments