0

I understand that SimpleXML is far more efficient than DOMDocument. Any advice on how I would reform the below into a SimpleXML version?

    <?php
  $doc = new DOMDocument();
  $doc->load( 'feedpage.xml' );


  $Main = $doc->getElementsByTagName( "varOne" );
  foreach( $Main as $varOne )
  {
  $VarTwo = $varOne->getElementsByTagName( "VarTwo" );
  $VarTwo = $VarTwo->item(0)->nodeValue;

  $VarThree = $varOne->getElementsByTagName( "VarThree" );
  $VarThree = $VarThree->item(0)->nodeValue;

  $VarFour = $varOne->getElementsByTagName( "VarFour" );
  $VarFour = $VarFour->item(0)->nodeValue;

  $VarFive = $varOne->getElementsByTagName( "VarFive" );
  $VarFive = $VarFive->item(0)->nodeValue;

  echo "$VarTwo - $VarThree - $VarTFour - ETC\n";
  echo "<img src=\"$VarFive\" />";
  echo "<a href=\"$VarFour\" target=\"_blank\">Link</a>";
  }
  ?>
Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
  • Title A description A Link An Image I think I've missed one node there, but that's all it is for practise –  Feb 08 '12 at 21:17

1 Answers1

0

Start with something like this:

$doc = simplexml_load_file("feedpage.xml");

Then (since I don't know what your XML file looks like), try:

echo "<pre>".print_r($doc,true)."</pre>";

to see exactly how the resulting object is laid out. From there, you should be able to pick out the pieces you need to build what you want.

Edit:

If your output is:

SimpleXMLElement Object ( 
    [varOne] => SimpleXMLElement Object ( 
        [varOne] => Title 
        [varTwo] => A description 
        [VarThree] => A Link 
        [VarFour] => An Image
    )
)

You could do this to access the properties of each one:

foreach($doc as $row) {
    $title = $row->varOne;
    $description = $row->varTwo;
    // etc.
}

So with the foreach loop, you can go through each main item and access each item's properties.

And if you want to put code in comments, you can use the backtick (`) to surround your text, but it doesn't work well for code blocks (like the one you wanted to post). Best for variables or other short bits.

FINAL EDIT:

Let's take this example object:

SimpleXMLElement Object ( 
    [varOne] => SimpleXMLElement Object ( 
        [varOne] => "Title"
        [varTwo] => "Description"
        [varThree] => "Link"
        [varFour] => "Image"
        [varFive] => SimpleXMLElement Object (
            [varA] => "something"
            [varB] => SimpleXMLElement Object (
                [varX] => "a string"
            )
            [varC] => "another thing"
        )
    )
)

Let's say the whole thing is contained in a variable $obj. Now let's say we wanted what's in varX. We'd access it like this:

echo $obj->varOne->varFive->varB->varX;

Beyond this, I don't know what else to tell you. You need to closely examine the objects you have and determine how they are structured. Extrapolate what you've learned here and apply it to your situation.

WWW
  • 9,734
  • 1
  • 29
  • 33
  • Hi, (not sure about posting code into comments here... ) Thanks for that, right, I see, the response is: SimpleXMLElement Object ( [varOne] => SimpleXMLElement Object ( [varOne] => Title [varTwo] => A description [VarThree] => A Link [VarFour] => An Image ) ) I think there are 2 parts, how to loop through a number of xml *parts*, as in loop through and output - OK, I sort see this... thanks. But how do I access each node? –  Feb 08 '12 at 21:23
  • wow, thats a really useful feature: print_r..., thanks for that –  Feb 08 '12 at 21:28
  • @DwDws: I updated my answer with your object output, hopefully this makes more sense. – WWW Feb 08 '12 at 21:37
  • Thanks! I'll just take a few minutes to look over and quick test... thanks for your help –  Feb 08 '12 at 21:40
  • I'm sorry, I get a Invalid argument supplied for foreach() - that list is an array by my understanding by my quick look online. The code is: `$xml = simplexml_load_file('feedpage.xml'); foreach($doc as $row) { $title = $row->varOne; $description = $row->varTwo; $link = $row->varThree; $image = $row->varFour; (not sure about this line, anyway) echo "$title - $description - $link - $image\n"; } ?> ` –  Feb 08 '12 at 21:59
  • @DwDws: Because you didn't define `$doc`, you called the variable `$xml` for some reason. – WWW Feb 08 '12 at 22:07
  • Yes, sorry my brash copy/paste, $xml belongs to the demo below, no error, OK - so I did an echo on those variable, and I only get the 1st 2 echos - PS. my XML above was not quite correct I believe, it has the same node name for parent and child, changed parent to 'varOneHome' `echo $title; echo $description; echo $link; echo $image;` I also doubled up the XML to see if it would loop through, it did loop through, but again, only for the 1st 2 echo –  Feb 08 '12 at 22:20
  • PS, I confirm that I expanded the foreach with: `$title = $row->varOne; $description = $row->varTwo; $link = $row->varThree; $image = $row->varFour;` –  Feb 08 '12 at 22:25
  • You know what, it the CASE in the nodes, incorrect, so it was mismatched... that works now, I'm really grateful to you for taking the time to help me –  Feb 08 '12 at 22:49
  • You need to check your object names. The main object (in this case `$doc`) gets stepped through using `foreach()`. Each object contained within is accessed like any other PHP object: using the `->` notation to access properties and methods. The code as I wrote it works, I just don't necessarily have the property names right for your exact situation. You need to apply the concept I've given you to your situation. – WWW Feb 08 '12 at 22:49
  • @DwDws: No worries, I'm glad to help, though taking away the points you awarded me by marking the other answer wasn't very nice. – WWW Feb 08 '12 at 22:50
  • Ah I see! I thought it was a mark of completion rather than award, the tick I think. I hope I can correct this –  Feb 08 '12 at 22:54
  • re: your final advice, I had a quick look at the 'object operator' and sort of understood the idea. Sure, I understand what you're telling me. I think that SimpleXML is, of course, very good –  Feb 08 '12 at 23:00
  • Crontab, how would I further extend your PHP to access 1st and 2nd child nodes?? I've looked into `foreach($sxe->xpath('//RECORD') as $item) { ...` from the manual, but can't get it to work, any further tips please? –  Feb 09 '12 at 19:59
  • You don't need to use xpaths, just stick to the SimpleXML object. I've updated my answer with a final edit; there isn't anything else I can tell you about how to do this. – WWW Feb 09 '12 at 21:38
  • I see, so it partly works through each node to get the value. Thank you –  Feb 09 '12 at 22:51