我从第三方服务器获得以下XML响应:
<?xml version="1.0" encoding="utf-8" ?><Search type="upcDetails">
<Error>
<ErrorCode>0</ErrorCode>
<ErrorMessage>SUCCESS</ErrorMessage>
</Error>
<Product>
<Name><![CDATA[CGC UltraLight Gypsum 4'x8'x1/2"]]></Name>
<ProductId><![CDATA[1000686028]]></ProductId>
<storeAvailabilityText><![CDATA[Mississauga Britannia Store Qty: 806]]></storeAvailabilityText>
<OriginalPrice><![CDATA[6.49 ]]></OriginalPrice><UOM><![CDATA[EA]]></UOM>
<IsOnline><![CDATA[true]]></IsOnline>
</Product>
</Search>
然后我做$simpleXML = simplexml_load_string($response);
然后print_r($ simpleXML),我得到的回应是:
SimpleXMLElement Object ( [@attributes] => Array ( [type] => upcDetails ) [Error] => SimpleXMLElement Object ( [ErrorCode] => 0 [ErrorMessage] => SUCCESS ) [Product] => SimpleXMLElement Object ( [Name] => SimpleXMLElement Object ( ) [ProductId] => SimpleXMLElement Object ( ) [storeAvailabilityText] => SimpleXMLElement Object ( ) [OriginalPrice] => SimpleXMLElement Object ( ) [UOM] => SimpleXMLElement Object ( ) [IsOnline] => SimpleXMLElement Object ( ) ) )
当值本身是另一个SimpleXML对象时,如何访问例如[ProductId] => SimpleXMLElement Object ( )
的值?
答案 0 :(得分:1)
SimpleXMLElement
实现了__toString()
魔术方法,因此如果您在字符串上下文中使用$simpleXML->Product->ProductId
或明确地强制转换它,那么您将获得子文本元素。< / p>
var_dump((string) $simpleXML->Product->ProductId);
string(10) "1000686028"
答案 1 :(得分:0)
你可以这样尝试
$xml = simplexml_load_string($response);
echo $xml->Product->ProductId;