从xml字符串中获取内部值[复制]

时间:2022-07-20 23:39:59

This question already has an answer here:

这个问题在这里已有答案:

Hi guys how can I pull the amount value from the xml below?

嗨伙计们,我如何从下面的xml中提取金额?

<balance><amount value="1000" currencyCode="GBP" exponent="2" debitCreditIndicator="credit"/></balance>

I am currently using the following

我目前正在使用以下内容

$lastEvent = balance->amount->value;

1 个解决方案

#1


1  

The formatted XML would look like:

格式化的XML看起来像:

<balance>
   <amount value="1000" currencyCode="GBP" exponent="2" debitCreditIndicator="credit" />
</balance>

As you can see, value is not a separate node, so you'll have to access it as below (assuming you're using SimpleXML to do the parsing):

如您所见,value不是一个单独的节点,因此您必须按如下方式访问它(假设您使用SimpleXML进行解析):

$xml = simplexml_load_string($str); // $str contains your XML string
$lastEvent = $xml->amount['value'];

Demo!

演示!

#1


1  

The formatted XML would look like:

格式化的XML看起来像:

<balance>
   <amount value="1000" currencyCode="GBP" exponent="2" debitCreditIndicator="credit" />
</balance>

As you can see, value is not a separate node, so you'll have to access it as below (assuming you're using SimpleXML to do the parsing):

如您所见,value不是一个单独的节点,因此您必须按如下方式访问它(假设您使用SimpleXML进行解析):

$xml = simplexml_load_string($str); // $str contains your XML string
$lastEvent = $xml->amount['value'];

Demo!

演示!