开发者

Not able to read one particular XML node using PHP

开发者 https://www.devze.com 2023-04-12 16:12 出处:网络
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 bloc

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(SimpleXM开发者_Go百科LElement)#2 (1) {
    ["@attributes"]=>
    array(1) {
      ["src"]=>
      string(26) "images/interbank_img_0.jpg"
    }
  }
}


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号