spurrdog Posted August 3, 2010 Share Posted August 3, 2010 Hi, I'm new to php and coding in general. I'm trying to parse xml from a remote device and access specific value data. Here is the xml: <?xml version="1.0" encoding="ISO-8859-1" ?> - <Device id="S10011" hb="1935"> <Group id="1" /> <Group id="2" /> <Group id="3" /> <Group id="4" /> <Group id="5" /> <Group id="6" /> <Group id="7" /> <Group id="8" /> - <Group id="9"> - <Probe id="99"> <Value>1.0</Value> </Probe> - <Probe id="1"> <Value>86.4</Value> </Probe> - <Probe id="2"> <Value>45.7</Value> </Probe> - <Probe id="3"> <Value>2.9</Value> </Probe> - <Probe id="4"> <Value>1.0</Value> </Probe> </Group> </Device> ----------------------- Here is my php code to read in the xml: <?php // Establish a port 80 connection $http = fsockopen("192.168.2.106",80); // Send a request to the server $req = "GET /xmldata HTTP/1.0\r\n"; $req .= "Host: 192.168.2.106\r\n"; $req .= "Connection: Close\r\n\r\n"; fputs($http, $req); // Output the request results while(!feof($http)) { $data .= fgets($http, 1024); } // Close the connection fclose($http); $xml = simplexml_load_string($data); print_r ($xml); ?> This yields the following data: SimpleXMLElement Object ( [@attributes] => Array ( [id] => S10011 [hb] => 117546 ) [Group] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1 ) [0] => ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2 ) [0] => ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 3 ) [0] => ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 4 ) [0] => ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 5 ) [0] => ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 6 ) [0] => ) [6] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 7 ) [0] => ) [7] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 ) [0] => ) [8] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 9 ) [Probe] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 99 ) [Value] => 1.0 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1 ) [Value] => 84.2 ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2 ) [Value] => 44.1 ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 3 ) [Value] => 4.8 ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 4 ) [Value] => 1.0 ) ) ) ) ) I would like to display group 9 probe 1 value for example and I cannot get it to work. Any tips? Quote Link to comment https://forums.phpfreaks.com/topic/209663-help-with-simplexml/ Share on other sites More sharing options...
bh Posted August 3, 2010 Share Posted August 3, 2010 Yeah, and whats the problem. First use google, and if you stuck than ask a question: simple_xml_tutorial Anyway, if you know a specific element what you want to get use xpath. Quote Link to comment https://forums.phpfreaks.com/topic/209663-help-with-simplexml/#findComment-1094589 Share on other sites More sharing options...
AbraCadaver Posted August 3, 2010 Share Posted August 3, 2010 And the code would be shorter and easier using: $xml = simplexml_load_file('http://192.168.2.106/xmldata'); Quote Link to comment https://forums.phpfreaks.com/topic/209663-help-with-simplexml/#findComment-1094704 Share on other sites More sharing options...
spurrdog Posted August 3, 2010 Author Share Posted August 3, 2010 And the code would be shorter and easier using: $xml = simplexml_load_file('http://192.168.2.106/xmldata'); AbraCadaver, Thanks for the feedback. The reason I'm using sockets is the above statement generates this error that I have not been able to resolve. "Warning: simplexml_load_file(http://192.168.2.106/xmldata) [function.simplexml-load-file]: failed to open stream: HTTP request failed! <?xml version="1.0" encoding="ISO-8859-1"?> in C:\xampp\htdocs\xmltest2.php on line 2" I think it is related to the way this device implements http. Yeah, and whats the problem. First use google, and if you stuck than ask a question: simple_xml_tutorial Anyway, if you know a specific element what you want to get use xpath. bh, Thank you for your feedback. I've reviewed and tried many samples / tutorials and applied to this xml with no luck. For example the following xpath code does not yield the <value> for probe 1: <?php // Establish a port 80 connection $http = fsockopen("192.168.2.106",80); // Send a request to the server $req = "GET /xmldata HTTP/1.0\r\n"; $req .= "Host: 192.168.2.106\r\n"; $req .= "Connection: Close\r\n\r\n"; fputs($http, $req); // Output the request results while(!feof($http)) { $xmlstr .= fgets($http, 2048); } // Close the connection fclose($http); $xml = simplexml_load_string($xmlstr); $myValue = $xml->xpath('//Group[@ID="9"]/Probe[@ID="1"]/value'); echo $myValue[0]; Quote Link to comment https://forums.phpfreaks.com/topic/209663-help-with-simplexml/#findComment-1094723 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.