解析 XML 文件 - 标记是保留字
Parse XML File - Tag is Reserved Word
我正在尝试从 XML 文件中获取数据值。我能够使其他行正常,但我无法将 [asin][/asin] 标记之间的数据作为其在 PHP 中的保留字。我想知道解决方法是什么?
这是我的代码:
$xml = file_get_contents($request_url, false, $context);
$xml = simplexml_load_string($xml);
$item = $xml->Items->Item[0];
// ** this is my Problem **
$asin = htmlentities((string) $item->Asin);
// ** this is my Problem **
$title = htmlentities((string) $item->ItemAttributes->Title);
这是 xml 文件的一部分:
<Item>
<ASIN>B00TSUGXKE</ASIN>
<ParentASIN>B010BWYDYA</ParentASIN>
其他元素工作正常。只是这个标签 "Asin" 是 php 中的保留字,所以不能使用。有没有其他方法可以仅引用标签而不是 php 中的 "asin" 函数?
试试这个:
$asin = htmlentities((string) $item->{'ASIN'});
这应该适合你。
$asin = htmlentities((string) $item->{'ASIN'});
XML 区分大小写。我做了更多的测试,效果很好。
实际上是字母大小写的问题。在 XML 中它是大写 ASIN
但是你请求在 Camel 中。
应该是:
$asin = htmlentities((string) $item->ASIN);
我正在尝试从 XML 文件中获取数据值。我能够使其他行正常,但我无法将 [asin][/asin] 标记之间的数据作为其在 PHP 中的保留字。我想知道解决方法是什么?
这是我的代码:
$xml = file_get_contents($request_url, false, $context);
$xml = simplexml_load_string($xml);
$item = $xml->Items->Item[0];
// ** this is my Problem **
$asin = htmlentities((string) $item->Asin);
// ** this is my Problem **
$title = htmlentities((string) $item->ItemAttributes->Title);
这是 xml 文件的一部分:
<Item>
<ASIN>B00TSUGXKE</ASIN>
<ParentASIN>B010BWYDYA</ParentASIN>
其他元素工作正常。只是这个标签 "Asin" 是 php 中的保留字,所以不能使用。有没有其他方法可以仅引用标签而不是 php 中的 "asin" 函数?
试试这个:
$asin = htmlentities((string) $item->{'ASIN'});
这应该适合你。
$asin = htmlentities((string) $item->{'ASIN'});
XML 区分大小写。我做了更多的测试,效果很好。
实际上是字母大小写的问题。在 XML 中它是大写 ASIN
但是你请求在 Camel 中。
应该是:
$asin = htmlentities((string) $item->ASIN);