我有以下字符串:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/UKash/Service1"><UKashTransaction><txCode>99</txCode><txDescription>Failed</txDescription><settleAmount> <settleAmount><transactionId>1341481253EDFC871620</transactionId><changeIssueVoucherNumber> <changeIssueVoucherNumber><changeIssueVoucherCurr> <changeIssueVoucherCurr><changeIssueAmount> <changeIssueAmount><changeIssueExpiryDate> <changeIssueExpiryDate><ukashTransactionId> <ukashTransactionId><currencyConversion> <currencyConversion><errCode>219</errCode><errDescription>Invalid Voucher Number</errDescription> <UKashTransaction></string></xml>
我想获得该字符串的一些属性,比如txDescrition,等等,我尝试了不同的变体,但无法获得结果。我正在使用php,如果有人可以提供帮助,我会非常感激,因为我被封锁了。
更新:
问题是我没有将字符串格式化为有效所以我使用curl,这是我从网关得到的:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/UKash/Service1"><UKashTransaction><txCode>99</txCode><txDescription>Failed</txDescription><settleAmount></settleAmount><transactionId>13414821393ED286BF2A</transactionId><changeIssueVoucherNumber></changeIssueVoucherNumber><changeIssueVoucherCurr></changeIssueVoucherCurr><changeIssueAmount></changeIssueAmount><changeIssueExpiryDate></changeIssueExpiryDate><ukashTransactionId></ukashTransactionId><currencyConversion></currencyConversion><errCode>219</errCode><errDescription>Invalid Voucher Number</errDescription></UKashTransaction></string>
TNX。
答案 0 :(得分:1)
您的XML格式不正确 - 它以</xml>
结尾,其中没有起始<xml>
元素。假设你正在纠正这个问题,下面的代码将解决你的问题。
$xml = simplexml_load_string($string);
print_r($xml->txDescrition);
答案 1 :(得分:0)
如果你有简单的XML(PHP应该默认启用它),试试这个:
$xmlObject = simplexml_load_string($string);
echo $xmlObject->txDescription;
http://www.php.net/manual/en/function.simplexml-load-string.php