开发者

SimpleXML seems to not load anything...?

开发者 https://www.devze.com 2023-02-01 07:44 出处:网络
This is really baffling me. I\'ve never used XML before, so I\'m just playing about (it\'s the middle of the night of course...!)

This is really baffling me. I've never used XML before, so I'm just playing about (it's the middle of the night of course...!)

The code:

$xml_string = "<?xml version="1.0" encoding="utf-8" ?><result>
    <status>fail</status>
    <error>
        <code>192</code>
        <message>Previous command still being processing开发者_JS百科</message>
    </error>
</result>"
$rss = simplexml_load_string($xml_string);
print $rss->result->status;

I'm kinda baffled... Every tutorial that I have read seems to suggest that this will print "fail" - but yet it doesn't...? Can someone point me in the right direction?


There are some errors:

  • You are quoting your string in double quotes but there are unescaped double quotes inside the string which prematurely terminate your string. To fix this either escape the double quotes inside the string or use single quote or here doc.
  • The result node is the root of the document so to print fail you need:

    print $rss->status;
    

See it

As a tip you can always dump the content of the XML object by doing print_r($rss);

0

精彩评论

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