我有一个像这样的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Items>
<Item Category="14" />
</Items>
<Events>
<Event type="EventType">
<Date>November 24, 2016</Date>
</Event>
</Events>
</Data>
并希望在Item-&gt; Category中获得值“14”。
我尝试过这样:
echo $xml->Items->Item[0]->{@attributes}['Category'];
输出为空。
当我这样做时:
print_r($xml)
我得到:
SimpleXMLElement Object ( [Items] => SimpleXMLElement Object ( [Item] => SimpleXMLElement Object ( [@attributes] => Array ( [Category] => 14 ) ) ) [Events] => SimpleXMLElement Object ( [Event] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => EventType ) [Date] => November 24, 2016 ) ) )
然后我尝试了:
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
输出是:
Index array Array ( ) Vals array Array ( )
XML就像这样打开:
$file="test.xml";
if (file_exists($file)) {
$xml = simplexml_load_file($file);
答案 0 :(得分:0)
您可以使用以下代码获取它:
$xml = simplexml_load_file('folder/yourfile');
$xmlselect = $xml->Data;
foreach($xmlselect AS $objekt)
{
$items = $objekt->Items->Item;
foreach ($items as $key) {
$itemId= $key->attributes();
}
}
也许它会帮助你!