Jump to content

SimpleXML isn't being very simple for me. Need a little help please...???


Recommended Posts

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!

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>';
    }
}


?>

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!

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 ???

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.