I have a xml File like that:
<?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>
And want to get the Value "14" in the Item->Category.
I tried like that:
echo $xml->Items->Item[0]->{@attributes}['Category'];
Output is empty.
When i just do:
print_r($xml)
i get:
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 ) ) )
Then i tried:
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
echo "Index array
";
print_r($index);
echo "
Vals array
";
print_r($vals);
Output is:
Index array Array ( ) Vals array Array ( )
The XML is opened like that:
$file="test.xml";
if (file_exists($file)) {
$xml = simplexml_load_file($file);
you can get it with the following code:
$xml = simplexml_load_file('folder/yourfile');
$xmlselect = $xml->Data;
foreach($xmlselect AS $objekt)
{
$items = $objekt->Items->Item;
foreach ($items as $key) {
$itemId= $key->attributes();
}
}
Maybe it will help you!