sha
sha

Reputation: 1

Not able to read one particular XML node using PHP

I'm using PHP to parse an XML file, and am successfully able to pull data from the file, with the exception of one particular node. There is an open ended tag labeled "ImageData" followed by a block of text. The block of text is not getting picked up, and I can't figure out why its ignoring the text that follows the "ImageData" tag.

Here's the relevant XML code:

<Part>
<Figure>
<ImageData src="images/interbank_img_0.jpg"/>
The text I want is here
</Figure>

This is the PHP script:

$xml = simplexml_load_file("rates/interbank.xml");
$test = $xml->Part[0]->Figure[0];

Here's the result when outputting $test:

object(SimpleXMLElement)#3 (1) {
  ["ImageData"]=>
  object(SimpleXMLElement)#2 (1) {
    ["@attributes"]=>
    array(1) {
      ["src"]=>
      string(26) "images/interbank_img_0.jpg"
    }
  }
}

Upvotes: 0

Views: 268

Answers (1)

VolkerK
VolkerK

Reputation: 96159

Just convert the node to string

<?php
//$xml = simplexml_load_file("rates/interbank.xml");
$xml = new SimpleXMLElement('<foo><Part>
<Figure>
<ImageData src="images/interbank_img_0.jpg"/>
The text I want is here
</Figure></Part></foo>');

echo $xml->Part[0]->Figure[0]; // echo casts the "parameters" to string before printing

prints

The text I want is here

Upvotes: 1

Related Questions