PHP Parse XML response with many namespaces -


is there way parse through xml response in php, taking account namespaced nodes , convert object or array without knowing node names?

for example, converting this:

<?xml version="1.0" encoding="iso-8859-1"?> <serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service"     xmlns:com="http://www.webex.com/schemas/2002/06/common"     xmlns:att="http://www.webex.com/schemas/2002/06/service/attendee">     <serv:header>         <serv:response>             <serv:result>success</serv:result>             <serv:gsbstatus>primary</serv:gsbstatus>         </serv:response>     </serv:header>     <serv:body>         <serv:bodycontent xsi:type="att:lstmeetingattendeeresponse"             xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">             <att:attendee>                 <att:person>                     <com:name>james kirk</com:name>                     <com:firstname>james</com:firstname>                     <com:lastname>kirk</com:lastname>                     <com:address>                         <com:addresstype>personal</com:addresstype>                     </com:address>                     <com:phones />                     <com:email>jkirk@sz.webex.com</com:email>                     <com:type>visitor</com:type>                 </att:person>                 <att:contactid>28410622</att:contactid>                 <att:joinstatus>invite</att:joinstatus>                 <att:meetingkey>803754412</att:meetingkey>             </att:attendee>         </serv:bodycontent>     </serv:body> </serv:message> 

to like:

['message' => [     'header' => [         'response' => [             'result' => 'success',             'gsbstatus' => 'primary'         ]     ],     'body' => [         'bodycontent' => [             'attendee' => [                 'person' => [                     'name' => 'james kirk',                     'firstname' => 'james',                     ...                 ],                 'contactid' => 28410622,                 ...             ]         ]     ] ] 

i know it's easy non-namespaced nodes, don't know begin on this.

(read @thw's answer why array not important aim for)

i know it's easy non-namespaced nodes, don't know begin on this.

it's easy namespaced nodes because technically same. let's give quick example, following script loops on elements in document regardless of namespace:

$result = $xml->xpath('//*'); foreach ($result $element) {     $depth = count($element->xpath('./ancestor::*'));     $indent = str_repeat('  ', $depth);     printf("%s %s\n", $indent, $element->getname()); } 

the output in case is:

 message    header      response        result        gsbstatus    body      bodycontent        attendee          person            name            firstname            lastname            address              addresstype            phones            email            type          contactid          joinstatus          meetingkey 

as can see can iterate on elements if not have namespace @ all.

but has been outlined, when ignore namespace you'll loose important information. example document have you're interested in attendee , common elements, service elements deal transport:

$uriatt = 'http://www.webex.com/schemas/2002/06/service/attendee'; $xml->registerxpathnamespace('att', $uriatt);  $uricom = 'http://www.webex.com/schemas/2002/06/common'; $xml->registerxpathnamespace('com', $uricom);  $result = $xml->xpath('//att:*|//com:*'); foreach ($result $element) {     $depth  = count($element->xpath("./ancestor::*[namespace-uri(.) = '$uriatt' or namespace-uri(.) = '$uricom']"));     $indent = str_repeat('  ', $depth);     printf("%s %s\n", $indent, $element->getname()); } 

the exemplary output time:

 attendee    person      name      firstname      lastname      address        addresstype      phones      email      type    contactid    joinstatus    meetingkey 

so why drop namespaces? obtain elements you're interested in. can dynamically


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -