desmod Posted November 15, 2013 Share Posted November 15, 2013 Currently I am able to use for-loop to get values for each pair of Channel and APcount because I know how many pairs are sent in XML. The "ResultNumberOfEntries" gives me that value and all is good. The question I have is, how would I be able to use for-loop if the ResultNumberOfEntries is not passed. Here is my code that works fine as long as ResultNumberOfEntries is known. <? $feed = file_get_contents("test.xml"); $xml = new SimpleXmlElement($feed); $xml->registerXPathNamespace("ns", "urn:broadband-forum-org:ipdr:tr-232-1-0"); $number_of_entries = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.ResultNumberOfEntries']/ns:Value"); $number_of_entries = $number_of_entries[0]; for ($i = 1; $i <= $number_of_entries; $i++) { $channel = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.{$i}.Channel']/ns:Value"); $channel = $channel[0]; $apcount = $xml->xpath("//ns:BulkData[ns:Name='InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.{$i}.APcount']/ns:Value"); $apcount = $apcount[0]; echo $channel.' : '.$apcount; echo "<br />"; } ?> Here is the test.xml file: <?xml version = "1.0" encoding = "UTF-8"?> <ipdr:IPDRDoc xmlns:ipdr="http://www.ipdr.org/namespaces/ipdr" xmlns="urn:broadband-forum-org:ipdr:tr-232-1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:broadband-forum-org:ipdr:tr-232-1-0 tr-232-1-0-0-serviceSpec.xsd http://www.ipdr.org/namespaces/ipdr http://www.ipdr.org/public/IPDRDoc3.5.1.xsd" docId="74697373-6f74-7878-7878-746973736f74" creationTime="2013-06-11T06:02:55.153Z" IPDRRecorderInfo="IPDR Collector" version="3.5.1"> <ipdr:IPDR xsi:type="BulkDataReport"> <BulkData> <Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.ResultNumberOfEntries</Name> <Value>3</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.1.Channel</Name> <Value>1</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.1.APcount</Name> <Value>3</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.2.Channel</Name> <Value>6</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.2.APcount</Name> <Value>8</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.3.Channel</Name> <Value>11</Value> </BulkData> <BulkData> <Name>InternetGatewayDevice.LANDevice.1.WLANConfiguration.X_181BEB_ChannelDiagnostics.Result.3.APcount</Name> <Value>6</Value> </BulkData> </ipdr:IPDR > <ipdr:IPDRDoc.End count="1" endTime="2013-06-11T06:02:55.207Z"></ipdr:IPDRDoc.End> </ipdr:IPDRDoc> This is the result I am expecting: 1 : 3 6 : 8 11 : 6 Quote Link to comment https://forums.phpfreaks.com/topic/283948-using-for-loop-when-xml-child-node-name-contains-variable/ Share on other sites More sharing options...
requinix Posted November 15, 2013 Share Posted November 15, 2013 Is that even a possibility? What request are you sending? I'm trying to find documentation about the response but it's practically impossible without knowing what the request is. Quote Link to comment https://forums.phpfreaks.com/topic/283948-using-for-loop-when-xml-child-node-name-contains-variable/#findComment-1458476 Share on other sites More sharing options...
desmod Posted November 16, 2013 Author Share Posted November 16, 2013 Sorry if my question was perhaps not clear. Lets say that I am getting XML data of cars sold on the end of each day and that looks like this: <?xml version = "1.0" encoding = "UTF-8"?> <ipdr:IPDRDoc xmlns:ipdr="http://www.ipdr.org/namespaces/ipdr" xmlns="urn:broadband-forum-org:ipdr:tr-232-1-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:broadband-forum-org:ipdr:tr-232-1-0 tr-232-1-0-0-serviceSpec.xsd http://www.ipdr.org/namespaces/ipdr http://www.ipdr.org/public/IPDRDoc3.5.1.xsd" docId="74697373-6f74-7878-7878-746973736f74" creationTime="2013-06-11T06:02:55.153Z" IPDRRecorderInfo="Car Collector" version="3.5.1"> <ipdr:IPDR xsi:type="CarDataReport"> <BulkData> <Name>car.1.make</Name> <Value>Honda</Value> </BulkData> <BulkData> <Name>car.1.model</Name> <Value>Civic</Value> </BulkData> <BulkData> <Name>car.2.make</Name> <Value>Toyota</Value> </BulkData> <BulkData> <Name>car.2.model</Name> <Value>Camry</Value> </BulkData> <BulkData> <Name>car.3.make</Name> <Value>Ford</Value> </BulkData> <BulkData> <Name>car.3.model</Name> <Value>Escort</Value> </BulkData> </ipdr:IPDR > <ipdr:IPDRDoc.End count="1" endTime="2013-06-11T06:02:55.207Z"></ipdr:IPDRDoc.End> </ipdr:IPDRDoc> So lets say that I want to get the values for each Car Make/Model. If XML will always show me data for 3 cars than I can use for-loop and use condition <? for ($i = 1; $i <= 3; $i++){ $car_make = $xml->xpath("//ns:BulkData[ns:Name='car.{$i}.make']/ns:Value"); $car_make = $car_make[0]; $car_model = $xml->xpath("//ns:BulkData[ns:Name='car.{$i}.model']/ns:Value"); $car_model = $car_model[0]; echo $car_make'. .'$car_model'. <br> .'; } ?> But since my XML will show me different number of cars, the script above will not work. So far I came with this workaround. I just put some high number that I know it will not be exceeded and than break the loop when $car_model value is empty. I was just hoping there is more elegant solution to my problem. <? for ($i = 1; $i <= 1000; $i++){ $car_make = $xml->xpath("//ns:BulkData[ns:Name='car.{$i}.make']/ns:Value"); $car_make = $car_make[0]; $car_model = $xml->xpath("//ns:BulkData[ns:Name='car.{$i}.model']/ns:Value"); $car_model = $car_model[0]; if ($car_model == ""){ break; } else { echo $car_make'. .'$car_model'. <br> .'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/283948-using-for-loop-when-xml-child-node-name-contains-variable/#findComment-1458498 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.