SimpleXML/PHP - Can't access object

2019-02-25 04:07发布

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = simplexml_load_string($tmp2);
echo $xml->RESPONSE->DATA->HEADER->COLUMN[0];

The above won't output anything, even though a var_dump is sucessful:

object(SimpleXMLElement)#2 (1) {
  ["RESPONSE"]=>
  object(SimpleXMLElement)#3 (1) {
    ["DATA"]=>
    object(SimpleXMLElement)#4 (1) {
      ["HEADER"]=>
      object(SimpleXMLElement)#5 (1) {
        ["COLUMN"]=>
        array(2) {
          [0]=>
          string(8) "interval"
          [1]=>
          string(13) "creative_name"
        }
      }
    }
  }
}

Thanks

5条回答
smile是对你的礼貌
2楼-- · 2019-02-25 04:35

You could try to suppress the errors and then iterate over them an check if you can fix them, like explained HERE, because I can't see an error in the example XML.

查看更多
爷、活的狠高调
3楼-- · 2019-02-25 04:37
   $tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';

    $xml = simplexml_load_string($tmp2);
    var_dump($xml);
object(SimpleXMLElement)#1 (1) {
  ["RESPONSE"]=>
  object(SimpleXMLElement)#2 (1) {
    ["DATA"]=>
    object(SimpleXMLElement)#3 (1) {
      ["HEADER"]=>
      object(SimpleXMLElement)#4 (1) {
        ["COLUMN"]=>
        array(2) {
          [0]=>
          string(8) "interval"
          [1]=>
          string(4) "name"
        }
      }
    }
  }
}
查看更多
我只想做你的唯一
4楼-- · 2019-02-25 04:46

The XML input is valid, I suppose the problem is with your PHP setup (old or buggy version). This is the output on my machine (PHP 5.3.8):

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = new SimpleXMLElement($tmp2);
var_dump($xml);

// output:
object(SimpleXMLElement)#1 (1) {
  ["RESPONSE"]=>
  object(SimpleXMLElement)#2 (1) {
    ["DATA"]=>
    object(SimpleXMLElement)#3 (1) {
      ["HEADER"]=>
      object(SimpleXMLElement)#4 (1) {
        ["COLUMN"]=>
        array(2) {
          [0]=>
          string(8) "interval"
          [1]=>
          string(4) "name"
        }
      }
    }
  }
}

EDIT after update of question:

This really must be your PHP version, this is the output on my machine again:

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = simplexml_load_string($tmp2);
echo $xml->RESPONSE->DATA->HEADER->COLUMN[0];

// output:
interval
查看更多
淡お忘
5楼-- · 2019-02-25 04:54
$tmp2 = <<<XML<?xml version="1.0" standalone='yes'?>
<RWResponse>
 <RESPONSE>
  <DATA>
   <HEADER>
    <COLUMN>interval</COLUMN>
    <COLUMN>name</COLUMN>
   </HEADER>
  </DATA>
 </RESPONSE>
</RWResponse>
XML;

would be the real way to do it, so whatever the XML input is, you would take and configure it so that it looks more like that so that it's not a 'string' but it's actually formulated XML information.

查看更多
可以哭但决不认输i
6楼-- · 2019-02-25 05:00

This is because SimpleXML requires exact typecasting or you'll get riddiculous things happening like this - var_dump will output what you want, echo won't. You always need to do this, there are even worse bugs, like echo ceil($simplexml->someNumber) will output 7 if the number is for example 7.85 and so on.
Try instead:

echo (string)$xml->RESPONSE->DATA->HEADER->COLUMN[0];
查看更多
登录 后发表回答