Trying to parse simple XML file to array for accessing variables

First off I will say I’m very new at PHP scripting so please forgive any unintelligent questions. I have an XML file that is an export from a program which I would like to parse into an array and call variables from. I seem to be getting stuck on part of calling variables from the array. I’m trying to do this with as little modification to the xml file as possible. I would like the script to be able to upload the file and parse it into a table and useable variables. I have the upload portion working and I have the table working in another script, now I just need to get the variables out of the xml.

Ideally I would like to be able to call them by the camera number (0 to whatever the file ends at), then be able to access the name, company, product, and so on.

Any Help on this would be great.

IQeye_Cameras.xml file.

<?xml version="1.0" encoding="utf-8"?> <IQmanager> <IsDetailsView>true</IsDetailsView> <Cameras> <camera name="IQEYE0152D8" company="IQinVision" product="IQeye701" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0152D8" ipAddr="3348174858" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0155EC" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.7/2(061113)" macAddr="00501A0155EC" ipAddr="1737562122" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE016169" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A016169" ipAddr="2039552010" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE016171" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A016171" ipAddr="1972443146" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE016174" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A016174" ipAddr="2123438090" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE016177" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A016177" ipAddr="1938888714" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE016180" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A016180" ipAddr="1955665930" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE01618D" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A01618D" ipAddr="2156992522" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE017999" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A017999" ipAddr="2089883658" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0179A1" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0179A1" ipAddr="2073106442" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0179A2" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0179A2" ipAddr="2056329226" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0179B0" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0179B0" ipAddr="1922111498" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0179BF" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0179BF" ipAddr="1989220362" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0179C1" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0179C1" ipAddr="2106660874" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0179C2" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0179C2" ipAddr="2005997578" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0179C5" company="IQinVision" product="IQeye703" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0179C5" ipAddr="2022774794" port="80" subnet="16711679" gateway="26220554" platform="59" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE018129" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A018129" ipAddr="1888557066" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE018203" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A018203" ipAddr="1855002634" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE018206" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A018206" ipAddr="1804670986" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE018285" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A018285" ipAddr="1838225418" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE018300" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A018300" ipAddr="1754339338" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE01837E" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A01837E" ipAddr="1871779850" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE018398" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A018398" ipAddr="1905334282" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0183C5" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A0183C5" ipAddr="1787893770" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE01854F" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A01854F" ipAddr="1771116554" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE018555" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A018555" ipAddr="2140215306" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE018571" company="IQinVision" product="IQeye511" firmwareVersion="Version V2.8/2(070504)" macAddr="00501A018571" ipAddr="1821448202" port="80" subnet="16711679" gateway="26220554" platform="60" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE02471A" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A02471A" ipAddr="2224101386" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE024722" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A024722" ipAddr="2341541898" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE024724" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A024724" ipAddr="2207324170" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE024728" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A024728" ipAddr="2358319114" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE024735" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A024735" ipAddr="2190546954" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE02474A" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A02474A" ipAddr="2291210250" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE02474F" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A02474F" ipAddr="2274433034" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE02475C" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A02475C" ipAddr="2173769738" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE02476F" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A02476F" ipAddr="2324764682" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE02483B" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A02483B" ipAddr="2307987466" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE0255B1" company="IQinVision" product="IQeye811" firmwareVersion="Version V2.8/6(080313)" macAddr="00501A0255B1" ipAddr="2240878602" port="80" subnet="16711679" gateway="26220554" platform="77" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE181B75" company="IQinVision" product="IQM31N" firmwareVersion="Version V3.2/4(101210)" macAddr="00501A181B75" ipAddr="3364886538" port="80" subnet="16711679" gateway="26220554" platform="107" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE181C41" company="IQinVision" product="IQM31N" firmwareVersion="Version V3.2/4(101210)" macAddr="00501A181C41" ipAddr="3398440970" port="80" subnet="16711679" gateway="26220554" platform="107" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE181C48" company="IQinVision" product="IQM31N" firmwareVersion="Version V3.2/4(101210)" macAddr="00501A181C48" ipAddr="3381663754" port="80" subnet="16711679" gateway="26220554" platform="107" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE2604AB" company="IQinVision" product="IQD31S" firmwareVersion="Version V3.2/3(101130)" macAddr="00501A2604AB" ipAddr="3415283722" port="80" subnet="16711679" gateway="26220554" platform="107" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE26089F" company="IQinVision" product="IQM31N" firmwareVersion="Version V3.2/4(101210)" macAddr="00501A26089F" ipAddr="3431995402" port="80" subnet="16711679" gateway="26220554" platform="107" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> <camera name="IQEYE260A21" company="IQinVision" product="IQM31N" firmwareVersion="Version V3.2/4(101210)" macAddr="00501A260A21" ipAddr="3415218186" port="80" subnet="16711679" gateway="26220554" platform="107" bootFlags="0" isLocal="true" isGsauce="true" isMissing="false" /> </Cameras> <Groups> <group>Groups</group> </Groups> </IQmanager>

XMLparsefile.php
[php]<?php
if (file_exists(‘IQeye_Cameras.xml’))
{
$xml = simplexml_load_file(‘IQeye_Cameras.xml’);

echo 'Begin testing calling of arrays</br>';
echo $xml->Cameras->camera[0]->name['value'];
echo 'End testing calling of arrays';	


echo"<pre>";
var_dump($xml);
echo"</pre>";

}

else
{
exit(‘Error.’);
}
?> [/php]

OK so I have gotten further in my attempt to make this work now I 'm stuck on getting the counter to loop with the output for each row. I figured out how to get the data into the table and I can loop the table to show the correct number of records in the xml I just can’t seem to get the counter showing which number each row is. I can get total number but not each row number.

Here is the code that is working so far.
[php]

<?php if (file_exists('IQeye_Cameras.xml')) { $xml = simplexml_load_file('IQeye_Cameras.xml'); $xmlNode = $xml->xpath('//camera'); $nodeCount = count($xmlNode); echo $nodeCount; /*** Create a tabe for the output ***/ echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; /*** Here is the loop for the rows ***/ foreach($xml->xpath("//camera") as $data){ $atts = $data->attributes(); echo ""; } } else { exit('Error.'); } ?> [/php]
CounterNameCompanyProductFirmware VersionMac Address
"; echo $i; /*** Need to show a counter here ***/ echo " "; echo $atts['name']; echo " "; echo $atts['company']; echo " "; echo $atts['product']; echo " "; echo $atts['firmwareVersion']; echo " "; echo $atts['macAddr']; echo "

If this is a counter - you need to increment it on each iteration of your loop. Just update this line in your code:
[php] echo $i;[/php]

to this:[php] echo $i++;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service