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>";
}
?>
Start with something like this:
Then (since I don’t know what your XML file looks like), try:
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:
You could do this to access the properties of each one:
So with the
foreachloop, 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:
Let’s say the whole thing is contained in a variable
$obj. Now let’s say we wanted what’s invarX. We’d access it like this: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.