Jump to content

Parsing Xml Child Nodes


scottish_jason

Recommended Posts

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 by scottish_jason
Link to comment
Share on other sites

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 by scottish_jason
Link to comment
Share on other sites

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"));
}

Link to comment
Share on other sites

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 by scottish_jason
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.