Parse complex XML with PHP



I am not a programming ninja, my specialties are in other things. But I need to take a complex (and large ~.5-3mb) XML file and extract certain values from it, add HTML, and spit it back out on to a webpage.


The part that has me particularly hung up is best explained by the example below:


There are hundreds of elements that look like this:



<device id="1023">


each with a different number for their "id" attribute.


Nested inside each of those elements are several elements like this:



<sensor id="34222"> each of those also has a different number for their "id" attribute.


Now what I need to do is two fold. I need to go to specific elements with a certain "id" attribute, and retrieve the value of a sub element like . Then as a separate task I need to go to specific "sensor" elements with a certain "id" attribute, and retrieve the value of a sub element like .


I will have the specific "id" number for the device and sensors I need to query, so it will be statically defined. I need to grab that data so I can write it and some HTML code to some sort of variable or spit it back as the data returning from the PHP execution.


Additionally, before you even get to any of the or there are several tags/elements, and some of them have attributes or "id"s. Some or elements have multiple tags/elements with different attribute "id" numbers. A sample of the XML file is below.


Is what I'm trying to do even possible? This has my brain all twisted around. Thanks for any help!!



<bldghvac>
<bldghvac-version>14.2.10.2114+</bldghvac-version>
<options>...</options>
<sensortree>
<nodes>
<probenode id="20199">
<name>Customer 1 (123.43.66.789)</name>
<id>20199</id>
<url>/probenode.htm?id=20199</url>
<tags/>
<priority>3</priority>
<fixed>0</fixed>
<status_raw>3</status_raw>
<active>true</active>

<group id="20255">
<name>Some Town</name>
<id>20255</id>
<url>/group.htm?id=20255</url>
<tags/>
<priority>3</priority>
<fixed>0</fixed>
<status_raw>3</status_raw>
<active>true</active>

<group id="21717">
<name>Group 1</name>
<id>21717</id>
<url>/group.htm?id=21717</url>
<tags/>
<priority>3</priority>
<fixed>0</fixed>
<status_raw>3</status_raw>
<active>true</active>

<device id="20357">
<summary>0,13,0,0,0,0,0,0</summary>
<name>Dev1 Bldg 1</name>
<deviceicon>vendors.png</deviceicon>
<id>20357</id>
<url>/device.htm?id=20357</url>
<tags>vendors</tags>
<priority>3</priority>
<fixed>0</fixed>
<host>172.22.37.253</host>
<status_raw>3</status_raw>
<active>true</active>

<sensor id="20359">
<name>System Uptime</name>
<id>20359</id>
<url>/sensor.htm?id=20359</url>
<fixed>0</fixed>
<status_raw>3</status_raw>
<status>Up</status>
<datamode>0</datamode>
<lastvalue>58 Days</lastvalue>
<active>true</active>
</sensor>

<sensor id="20359">
<name>System Uptime</name>
<id>20359</id>
<url>/sensor.htm?id=20359</url>
<fixed>0</fixed>
<status_raw>3</status_raw>
<status>Up</status>
<datamode>0</datamode>
<lastvalue>58 Days</lastvalue>
<active>true</active>
</sensor>

No comments:

Post a Comment