0

I am using the PHP library SimpleXML to pull data from an existing XML file on my site and add it to an HTML page.

The following is a snippet from the XML, which is at https://media.vivastaff.com/designs/94341/xml-3042-1.xml:

<DTilt>
    <frontSide imagePath="94341/20130510150043_ACXZ0001C-01.png"></frontSide>
    <backSide imagePath="94341/20130510150043_ACXZ0001C-02.png"></backSide>
</DTilt>

The following is the PHP code I'm using to pull the XML data:

<?php
$eid = 94341;
$pid = 3042;
$ver = 1;
$xmlPath =  'https://media.vivastaff.com/designs/' . $eid . '/xml-' . $pid . '-' . $ver . '.xml';
?>

<div id="cardFront"><img src="https://media.vivastaff.com/designs/<?php echo simplexml_load_file("$xmlPath")->DTilt->frontSide->imagePath; ?>"</div>

It is not pulling the data from the XML file I have and is returning nothing. I've tried many variations of this, but nothing seems to work. I may not be specifying "imagePath" correctly, but not sure were to go from here. What am I doing wrong on this?

Prix
  • 19,417
  • 15
  • 73
  • 132
Matto
  • 236
  • 3
  • 12
  • You are basically asking how to access an attribute node in simplexml: [How to get an attribute with SimpleXML?](http://stackoverflow.com/q/3410520/367456) - And before using simplexml read about the basic usage: http://php.net/simplexml.examples-basic – hakre Jun 12 '13 at 08:11

1 Answers1

1

Make sure your URL works, besides that the below code works just fine to read the above mentioned XML data.

<?php
$eid = 94341;
$pid = 3042;
$ver = 1;
$xmlPath =  'https://media.vivastaff.com/designs/' . $eid . '/xml-' . $pid . '-' . $ver . '.xml';

$dom = new DOMDocument();
$dom->load($xmlPath);
$frontSide = $dom->getElementsByTagName('frontSide');
$image = $frontSide->item(0)->getAttribute('imagePath');
?>
<div id="cardFront"><img src="https://media.vivastaff.com/designs/<?php echo $image; ?>"</div>
Prix
  • 19,417
  • 15
  • 73
  • 132
  • That worked perfectly. Guess I need to use the DOM to load it instead of simplexml_load_file. Thanks. – Matto Jun 12 '13 at 01:41
  • @Matto I have no experience with SimpleXML library but I've used DOM for a long time and on top of being built-in in most PHP builds it's easy to work with. Glad it worked for you. – Prix Jun 12 '13 at 01:43
  • Yes, with SimpleXML it's a two or one-liner, but it's not as distinct and flexible as DOMDocument. But both are close sister-libraries and as usual there is more than one way to do something. – hakre Jun 12 '13 at 08:09
  • @hakre yes indeed there is more than one way to do something, I mostly posted the DOM method because the OP didn't say that he wanted it specifically with SimpleXML otherwise I might have tried to adventure myself with it. – Prix Jun 12 '13 at 21:46
  • @Prix: I just was leaving the comment because OP commented about simplexml. DOM is fine, too. You can even do both because you can exchange nodes between the two libraries. – hakre Jun 12 '13 at 22:40