scottish_jason Posted October 14, 2012 Share Posted October 14, 2012 (edited) Hi everyone, I was wondering if anybody could help me with some XML parsing.... I have managed to parse most of the XML into variables but im struggling with one of the nodes where it is a second child : node[1] here is a snippet of the tree ($ndiscovery) SimpleXMLElement Object ( [@attributes] => Array ( [starttime] => 1350215881 [endtime] => 1350215891 ) [status] => SimpleXMLElement Object ( [@attributes] => Array ( [state] => up [reason] => arp-response ) ) [address] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [addr] => 192.168.0.1 [addrtype] => ipv4 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [addr] => A0:21:B7:06:20:67 [addrtype] => mac ) ) ) and a snippet of my code to parse ( where the problem seems to arise ) $discovery = simplexml_load_file('discovery.xml') foreach ($discovery->host as $ndiscovery) if ($ndiscovery->status->attributes()->state == up) { $inum++; $ip[$ipnum]=$ndiscovery->address[0]->attributes()->addr; $mac[$ipnum]=$ndiscovery->address[1]->attributes()->addr; etc... etc... if I remove the $mac line everything is fine, but if I leave it in there it seems to stop the rest of the webpage executing (after the php code finishes) the $mac line does actually work for retrieving the node but it seems to stop the rest of the page executing, very confused at this point. I had to use the [1] because there are 2 node's that are named the same (addr) Anybody have any ideas? thanks very much Edited October 14, 2012 by scottish_jason Quote Link to comment https://forums.phpfreaks.com/topic/269458-parsing-xml-child-nodes/ Share on other sites More sharing options...
requinix Posted October 14, 2012 Share Posted October 14, 2012 There's probably an easier way of doing this. What's the XML? Quote Link to comment https://forums.phpfreaks.com/topic/269458-parsing-xml-child-nodes/#findComment-1385191 Share on other sites More sharing options...
scottish_jason Posted October 14, 2012 Author Share Posted October 14, 2012 (edited) There's probably an easier way of doing this. What's the XML? The XML in question is there on the post, the full XML file is way too large to post up. edit: or if you are asking what the XML data actually is then its output from a linux program called nmap... it is a network discovery application showing ip addresses, mac addresses, open ports etc etc. Edited October 14, 2012 by scottish_jason Quote Link to comment https://forums.phpfreaks.com/topic/269458-parsing-xml-child-nodes/#findComment-1385192 Share on other sites More sharing options...
requinix Posted October 14, 2012 Share Posted October 14, 2012 Okay, I'll just reverse-engineer it from the code. <something> <host starttime="1350215881" endtime="1350215891"> <status state="up" reason="arp-response" /> <address addr="192.168.0.1" addrtype="ipv4" /> <address addr="A0:21:B7:06:20:67" addrtype="mac" /> </host> </something> xpath() can make the searching easier, and remove the need to rely on the ordering of the nodes (ie, [0] is the IPv4 address and [1] is the MAC address). $discovery = simplexml_load_file('discovery.xml'); $ipnum = 0; $ip = $mac = array(); foreach ($discovery->xpath("//host[status/@state='up']") as $ndiscovery) { $ipnum++; $ip[$ipnum] = (string)current($ndiscovery->xpath("address[@addrtype='ipv4']/@addr")); $mac[$ipnum] = (string)current($ndiscovery->xpath("address[@addrtype='mac']/@addr")); } Quote Link to comment https://forums.phpfreaks.com/topic/269458-parsing-xml-child-nodes/#findComment-1385195 Share on other sites More sharing options...
scottish_jason Posted October 14, 2012 Author Share Posted October 14, 2012 (edited) Okay, I'll just reverse-engineer it from the code. <something> <host starttime="1350215881" endtime="1350215891"> <status state="up" reason="arp-response" /> <address addr="192.168.0.1" addrtype="ipv4" /> <address addr="A0:21:B7:06:20:67" addrtype="mac" /> </host> </something> xpath() can make the searching easier, and remove the need to rely on the ordering of the nodes (ie, [0] is the IPv4 address and [1] is the MAC address). $discovery = simplexml_load_file('discovery.xml'); $ipnum = 0; $ip = $mac = array(); foreach ($discovery->xpath("//host[status/@state='up']") as $ndiscovery) { $ipnum++; $ip[$ipnum] = (string)current($ndiscovery->xpath("address[@addrtype='ipv4']/@addr")); $mac[$ipnum] = (string)current($ndiscovery->xpath("address[@addrtype='mac']/@addr")); } Thankyou very much for the reply but im afraid with that code the page now fails to load, not really sure why as im not really adept with xpath or XML in general to be honest, hehe I have been removing parts to try and find out which line is causing the problem and I cant even get the page to load with only this part in foreach ($discovery->xpath("//host[status/@state='up']") as $ndiscovery) { } endforeach; Edited October 14, 2012 by scottish_jason Quote Link to comment https://forums.phpfreaks.com/topic/269458-parsing-xml-child-nodes/#findComment-1385200 Share on other sites More sharing options...
requinix Posted October 14, 2012 Share Posted October 14, 2012 And that endforeach is coming from... where? Quote Link to comment https://forums.phpfreaks.com/topic/269458-parsing-xml-child-nodes/#findComment-1385205 Share on other sites More sharing options...
scottish_jason Posted October 14, 2012 Author Share Posted October 14, 2012 And that endforeach is coming from... where? oh damnit, im very sorry.... the fact I didnt sleep last night pretty much says it all! thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/269458-parsing-xml-child-nodes/#findComment-1385206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.