PHP 使用 PHPSimpleXML 从 XML 获取数据

我正在尝试从 xml 文件获取数据,但遇到了麻烦,因为该表的级别比我能找到的示例要多一些。


我希望能够迭代<Event>as的每个实例<Information>,并且<Events>仅打开和关闭数据。<Event>根据记录的事件数量重复。


表结构的示例是:


<?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>

PHP代码是:


<!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>


浮云间
浏览 126回答 2
2回答

繁星点点滴滴

您必须使用事件$xml->Events->Event as $events例如$xml=simplexml_load_file("data.xml") or die("Error: Cannot create object");foreach($xml->Events->Event as $events) {&nbsp; &nbsp; $id = $events->PrimaryObject->attributes();&nbsp; &nbsp; $name = $events->PrimaryObject->Name;&nbsp; &nbsp; echo $id['ID'].' '. $name;&nbsp; &nbsp; echo "<br>";}输出487 Player1PHP演示

慕桂英546537

我不确定您到底在寻找什么数据,但这里有所有内容,使用 xpath,您可以选择:$events = $xml->xpath('.//Event');foreach($events as $event) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $dat = $event->xpath('./PrimaryObject')[0];&nbsp; &nbsp; $time= $event->xpath('./Time');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $id = $dat->xpath('./@ID');&nbsp; &nbsp; $name = $dat->xpath('./Name');&nbsp; &nbsp; $country = $dat->xpath('./Country');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $dat2 = $event->xpath('./SecondaryObject')[0];&nbsp; &nbsp; $action= $event->xpath('./Action');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $id2 = $dat2->xpath('./@ID');&nbsp; &nbsp; $name2 = $dat2->xpath('./Name');&nbsp; &nbsp; $parent = $dat2->xpath('./Parent');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo 'Time: ' . $time[0];&nbsp; &nbsp; echo "<br>";&nbsp;&nbsp; &nbsp; echo 'Action: ' . $action[0];&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo "<br>";&nbsp;&nbsp; &nbsp; echo 'Primary Object Data:';&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo 'ID: ' . $id[0];&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; echo 'Name: ' . $name[0];&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; echo 'Country: ' . $country[0];&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp;&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; echo 'Secondary Object Data:';&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo 'ID: ' . $id2[0];&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; echo 'Name: ' . $name2[0];&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; echo 'Parent: ' . $parent[0];&nbsp; &nbsp; echo "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }输出:Time: 3141.29Action: MovePrimary Object Data:ID: 487Name: Player1Country: usSecondary Object Data:ID: 814Name: DogParent: 487
打开App,查看更多内容
随时随地看视频慕课网APP