PHP and XPath Loop

Posted by user1794852 on Stack Overflow See other posts from Stack Overflow or by user1794852
Published on 2012-12-04T23:01:28Z Indexed on 2012/12/04 23:03 UTC
Read the original article Hit count: 230

Filed under:
|
|
|
|

Thank you all in advance. I've got some great answers to my sometimes stupid questions, so thank you again.

I'm trying to parse a SOAP response using PHP, Xpath (namespaces) and SimpleXML. Down below is a snippet of the Response. What I need to do is loop through each <ns1:file></ns1:file> and add it to a DB. But I'm not sure how to do that. Please help!

Namespace Stuff

                $x = simplexml_load_string($response);    
                $x->registerXPathNamespace('ns1', 'http://ws.icontent.idefense.com/V3/2');

Here's the response:

          <ns1:mal_files>
              <ns1:file>
                 <ns1:id>2895144</ns1:id>
                 <ns1:md5>2189c3d3857ba0cabd19c8aa031d63cd</ns1:md5>
                 <ns1:sha1>c20b26148caa059ecf85e9b29df4e28e8354d655</ns1:sha1>
                 <ns1:path>%WINDIR%\Temp\Temporary Internet Files\Content.IE5\K9ANOPQB\1219831[1].htm</ns1:path>
                 <ns1:size>110</ns1:size>
                 <ns1:code_available>true</ns1:code_available>
                 <ns1:len_char>fixed</ns1:len_char>
              </ns1:file>
              <ns1:file>
                 <ns1:id>2895147</ns1:id>
                 <ns1:md5>a533825ef1752630a300125b3eef6825</ns1:md5>
                 <ns1:sha1>ec7feb1414b3c2e720f6c06e2750421b73634f87</ns1:sha1>
                 <ns1:path>%WINDIR%\Temp\Temporary Internet Files\Content.IE5\4PMB8T67\bg[1].jpg</ns1:path>
                 <ns1:size>707</ns1:size>
                 <ns1:code_available>true</ns1:code_available>
                 <ns1:len_char>fixed</ns1:len_char>
              </ns1:file>
              <ns1:file>
                 <ns1:id>2895155</ns1:id>
                 <ns1:md5>c88724e985efcc82173c0d3aa0b77dfd</ns1:md5>
                 <ns1:sha1>ecc042ca06aac988cd4593bb2b25fa39c4b2a819</ns1:sha1>
                 <ns1:path>%WINDIR%\Prefetch\24604775.EXE-3ADEC0C2.pf</ns1:path>
                 <ns1:size>44940</ns1:size>
                 <ns1:code_available>true</ns1:code_available>
                 <ns1:len_char>fixed</ns1:len_char>
              </ns1:file>
              <ns1:file>
                 <ns1:id>2895158</ns1:id>
                 <ns1:md5>422a011793af6195ea517c2c4a26bdbc</ns1:md5>
                 <ns1:sha1>f449240c091c27e2531342d6b291d6a7e4655834</ns1:sha1>
                 <ns1:path>%WINDIR%\Temp\Temporary Internet Files\Content.IE5\KPYV0TM7\gamesleap[1].png</ns1:path>
                 <ns1:size>2676</ns1:size>
                 <ns1:code_available>true</ns1:code_available>
                 <ns1:len_char>fixed</ns1:len_char>
              </ns1:file>
         </ns1:mal_files>

PHP Code

$md5 = $x->xpath('//ns1:mal_files/ns1:file/ns1:md5'); // Returns array :(

The PHP code returns an Array of all MD5 for every <ns1:file></ns1:file> and that's not what I need.

What I need is to loop through each of the (nodes?) for each of the <ns1:file> keying on the <ns1:id> and add all their associated elements (id, md5, sha1, etc) as a record, then move onto the next <ns1:file> and repeat. And obviously the PHP I'm using returns an array, which doesn't work here.

Hopefully I've made my question clear... I appreciate any help.

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about soap