TransWikia.com

PHP get data form XML using PHPSimpleXML

Stack Overflow Asked by XenPanda on December 18, 2021

I am trying to get data from an xml file and am having trouble as the table has a bit more levels than the examples I can find.

I want to be able to iterate through each instance of <Event> as <Information> and <Events> only open and close the data. The <Event> repeats based on the number of events logged.

A sample of the table structure is:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Information>
    <Events>
        <Event>
            <Time>3141.29</Time>
            <PrimaryObject ID="487">
                <Name>Player1</Name>
                <Country>us</Country>
            </PrimaryObject>
            <Action>Move</Action>
            <SecondaryObject ID="814">
                <Name>Dog</Name>
                <Parent>487</Parent>
            </SecondaryObject>
        </Event>
    </Events>
</Information>

The PHP code is:

<!DOCTYPE html>
<html>
<body>

<?php
$xml=simplexml_load_file("data.xml") or die("Error: Cannot create object");
foreach($xml->Event as $events) {
    $id = $events->PrimaryObject->attributes();
    $name = $events->PrimaryObject->Name;
    ...
    echo $id['ID'].' '. $name;
    echo "<br>";
  }
?>

</body>
</html>

2 Answers

You have to use the Events

$xml->Events->Event as $events

For example

$xml=simplexml_load_file("data.xml") or die("Error: Cannot create object");

foreach($xml->Events->Event as $events) {
    $id = $events->PrimaryObject->attributes();
    $name = $events->PrimaryObject->Name;
    echo $id['ID'].' '. $name;
    echo "<br>";
}

Output

487 Player1

Php demo

Answered by The fourth bird on December 18, 2021

I'm not sure what data exactly you are looking for, but here's everything, using xpath, and you can pick and choose:

$events = $xml->xpath('.//Event');
foreach($events as $event) {        
    $dat = $event->xpath('./PrimaryObject')[0];
    $time= $event->xpath('./Time');    
    $id = $dat->xpath('./@ID');
    $name = $dat->xpath('./Name');
    $country = $dat->xpath('./Country');
    
    $dat2 = $event->xpath('./SecondaryObject')[0];
    $action= $event->xpath('./Action');    
    $id2 = $dat2->xpath('./@ID');
    $name2 = $dat2->xpath('./Name');
    $parent = $dat2->xpath('./Parent');    
            
    echo 'Time: ' . $time[0];
    echo "<br>"; 
    echo 'Action: ' . $action[0];
    echo "<br>";    
    echo "<br>"; 
    echo 'Primary Object Data:';
    echo "<br>";    
    echo 'ID: ' . $id[0];
    echo "<br>";
    echo 'Name: ' . $name[0];
    echo "<br>";
    echo 'Country: ' . $country[0];
    echo "<br>";
    echo "<br>";   
    echo "<br>";
    echo 'Secondary Object Data:';
    echo "<br>";    
    echo 'ID: ' . $id2[0];
    echo "<br>";
    echo 'Name: ' . $name2[0];
    echo "<br>";
    echo 'Parent: ' . $parent[0];
    echo "<br>";         
  }

Output:

Time: 3141.29
Action: Move

Primary Object Data:
ID: 487
Name: Player1
Country: us


Secondary Object Data:
ID: 814
Name: Dog
Parent: 487

Answered by Jack Fleeting on December 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP