angelleye Posted May 25, 2007 Share Posted May 25, 2007 I'm working with the following XML: http://angelleye.sytes.net:8080/fmApps/fmresultset.xml The way I've found that's most comfortable for me to work (I know there may be better ways and I'll get there but for now I need to figure this out) is to select the group of nodes that I need to loop through and then do so, grabbing element values and doing what I need with that data inside the loop. I'm new to PHP and I'm having trouble figuring out how to do so. A quick explanation of the XML is that it comes from FileMaker Server and each <record> element is a record in the database. It's pretty straight forward...each field name is a child of <record> with a 'name' attribute, the value of which being the name of the field. Then a child of that is <data> and holds the value of that field in FileMaker. I'd like to loop through each <record> and write the value to the screen. I've got a quick little sample here with just a couple of the fields that I thought was going to work for me but it's not. <?php ini_set('display_errors', 1); error_reporting(E_ALL); $sXML = simplexml_load_file("fmresultset.xml"); $records = $sXML -> xpath("/fmresultset/resultset/record"); foreach($records as $record) { $Ref_Number = $record -> xpath("/field[@name='Ref_Number']/data"); //I thought this would grab the value but it doesn't. $Qty_Available = $record -> xpath("/field[@name='Qty_Available']/data"); //same here, no workie echo "Ref. Number: " . $Ref_Number . "<br /> Qty. Available: " . $Qty_Available . "<br />"; } ?> I'm getting a blank white browser page with no errors. Any information on what I'm doing wrong would be greatly appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/52913-simplexml-isnt-being-very-simple-for-me-need-a-little-help-please/ Share on other sites More sharing options...
Barand Posted May 25, 2007 Share Posted May 25, 2007 Does this help? <?php $sXML = simplexml_load_file("http://angelleye.sytes.net:8080/fmApps/fmresultset.xml"); foreach ($sXML->resultset as $record) { foreach ($record as $data) { echo '<table border="1">'; for($i=0; $i<10; $i++) { echo '<tr><td>', $data->field[$i]['name'], '</td><td>', $data->field[$i]->data,'</td></tr>'; } echo '</table><br><br>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52913-simplexml-isnt-being-very-simple-for-me-need-a-little-help-please/#findComment-261791 Share on other sites More sharing options...
angelleye Posted May 30, 2007 Author Share Posted May 30, 2007 Wow...how funny. I forgot to set the notify on this thread and never got a notification of a response. I've been pulling my hair out ever since with the php.net documentation and finally figured it out...for the most part. I played with almost exactly what you've done while I was simply printing the values to the screen. However, the primary thing I was trying to achieve was to get the values loaded into PHP variables that I could then use later. This would have helped me a ton had I seen it sooner. I'm back with more, though. I'm very close to having what I want except for one little thing. Here is what I currently have: <?php require_once('SimpleXMLElementExtended.php'); ?> <?php ini_set('display_errors', 1); error_reporting(E_ALL); $xml = simplexml_load_file('fmresultset.xml'); $xmlString = $xml -> saveXML(); $xmlExt = new SimpleXMLElementExtended($xmlString); foreach($xmlExt -> xpath('/fmresultset/resultset/record') as $record) { // for each record... foreach($record -> field as $field) { //for each field... $attName = $field -> getAttribute("name"); if($attName == "Ref_Number") $Ref_Number = $field -> data; elseif($attName == "Qty_Available") $Qty_Available = $field -> data; elseif($attName == "Purchase_Order_Items::Condition") $Condition = $field -> data; } echo "RefNumber: " . $Ref_Number . "<br /> Qty. Available: " . $Qty_Available . "<br /> Condition: " . $Condition . "<br /><br />"; } ?> As you can see that includes the class that was added on php.net by another user. The part I don't like is where the variables are actually getting populated. For every <record> node it's looping through each <field> element and checking the value of the 'name' attribute to find the correct one to load into that variable. This is an awful lot of looping, especially with larger datasets. When I do this ASP I'm able to use XPath to select exactly the element value I'm looking for w/o looping. Here's an ASP example of the same type of thing: Set Records = xmlDoc.selectNodes("/fmresultset/resultset/record") For Each record In Records Ref_Number = record.selectSingleNode("field[@name='Ref_Number']/data") Response.Write(Ref_Number & "<br />") Next Set RecordsNode = Nothing This creates an array of nodes using selectNodes with the same XPath I used in the PHP version. Then, for each <record> in this set I can use the selectSingleNode method along with XPath to grab exactly the value I'm looking for w/o having to loop through them all. I have this same exact thing working with DOM and I tried loading DOMXPath -> query($XPath) -> nodeValue but that would give me an unknown property error. Any information on how I can get this same thing to happen in PHP would be great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/52913-simplexml-isnt-being-very-simple-for-me-need-a-little-help-please/#findComment-264454 Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 Using a test newsfeed.xml file <?php $xml = simplexml_load_file('../kate/newsfeed.xml'); $ar = $xml->xpath('/rss/channel/item/title'); echo '<pre>', print_r($ar, true), '</pre>'; ?> gives--> Array ( [0] => SimpleXMLElement Object ( [0] => 1st Title ) [1] => SimpleXMLElement Object ( [0] => 2nd Title ) ) But with yours it just kept coming up empty ??? Quote Link to comment https://forums.phpfreaks.com/topic/52913-simplexml-isnt-being-very-simple-for-me-need-a-little-help-please/#findComment-264490 Share on other sites More sharing options...
angelleye Posted May 30, 2007 Author Share Posted May 30, 2007 whoops, forgot that domain wasn't working anymore. http://www.angelleye.com/phpTraining/XML_Parsing_Samples/fmresultset.xml Quote Link to comment https://forums.phpfreaks.com/topic/52913-simplexml-isnt-being-very-simple-for-me-need-a-little-help-please/#findComment-264501 Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 I managed to get your file OK and save it. It's just that that xpath wouldn't play. Quote Link to comment https://forums.phpfreaks.com/topic/52913-simplexml-isnt-being-very-simple-for-me-need-a-little-help-please/#findComment-264505 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.