从 XML 文件中获取节点在 php 中不起作用
Posted
技术标签:
【中文标题】从 XML 文件中获取节点在 php 中不起作用【英文标题】:Get nodes from XML file not working in php 【发布时间】:2014-05-26 22:10:48 【问题描述】:我有以下 XML 结构,需要显示 的时间戳和体育场名称、城市和国家/地区名称,但我的代码根本不起作用...这里有什么帮助吗?
<?xml version="1.0" encoding="utf-8"?>
<afpdb lang="fr-FR">
<head>
<message type="203" file="s4133-0000000-203-fr" timestamp="2014-04-16T12:16:26+02:00" />
</head>
<body>
<competition id="866" label="Brésil 2014">
<discipline code="FB" name="Football">
<evt id="4133" label="Brésil 2014" gender="M" date="2014-06-12T00:00:00-03:00">
<country iso="BRA" code="BRA" name="Brésil" />
<phase id="2717" code="TPFIN" type="PH1PT">
<group id="9296">
<match id="133114" num="63" status="EMNCO" day="1" timestamp="2014-07-12T17:00:00-03:00" dow="samedi" utc="2014-07-12T20:00:00+00:00">
<datas>
<stadium id="4499" name="Stade national Mané Garrincha">
<city id="2460" name="Brasilia">
<country iso="BRA" code="BRA" name="Brésil" />
</city>
</stadium>
</datas>
<res pos="1">
<team type="CETAB" display="NC" />
</res>
<res pos="2">
<team type="CETAB" display="NC" />
</res>
</match>
</group>
<group id="9297">
<match id="133115" num="64" status="EMNCO" day="1" timestamp="2014-07-13T16:00:00-03:00" dow="dimanche" utc="2014-07-13T19:00:00+00:00">
<datas>
<stadium id="172" name="Stade Maracana">
<city id="101" name="Rio de Janeiro">
<country iso="BRA" code="BRA" name="Brésil" />
</city>
</stadium>
</datas>
<res pos="1">
<team type="CETAB" display="NC" />
</res>
<res pos="2">
<team type="CETAB" display="NC" />
</res>
</match>
</group>
</phase>
<phase id="2716" code="TPSFI" type="PH1PT">
<group id="9294">
<match id="133112" num="61" status="EMNCO" day="1" timestamp="2014-07-08T17:00:00-03:00" dow="mardi" utc="2014-07-08T20:00:00+00:00">
<datas>
<stadium id="4412" name="Estadio Mineirão">
<city id="82" name="Belo Horizonte">
<country iso="BRA" code="BRA" name="Brésil" />
</city>
</stadium>
</datas>
<res pos="1">
<team type="CETAB" display="NC" />
</res>
<res pos="2">
<team type="CETAB" display="NC" />
</res>
</match>
</group>
<group id="9295">
<match id="133113" num="62" status="EMNCO" day="1" timestamp="2014-07-09T17:00:00-03:00" dow="mercredi" utc="2014-07-09T20:00:00+00:00">
<datas>
<stadium id="3040" name="Arena de São Paulo">
<city id="107" name="Sao Paulo">
<country iso="BRA" code="BRA" name="Brésil" />
</city>
</stadium>
</datas>
<res pos="1">
<team type="CETAB" display="NC" />
</res>
<res pos="2">
<team type="CETAB" display="NC" />
</res>
</match>
</group>
</phase>
</evt>
</discipline>
</competition>
</body>
</afpdb>
我在 php 中有什么可以尝试获取这些节点:
<?php
$document = new DOMDocument();
$document->load( "s4133-0000000-203-fr.xml" );
$phases = $document->getElementsByTagName( "phase" );
foreach( $phases as $phase )
$groups = $phase->getElementsByTagName( "group" );
foreach( $groups as $group )
$datas = $group->getElementsByTagName( "datas" );
echo $datas->stadium['name'];
echo $datas->stadium->city['name'];
echo $datas->stadium->city->country['name'];
?>
【问题讨论】:
【参考方案1】:您可以使用 XPath 来完成。在 XPath 结构中导航并获得所需的节点更容易。这个脚本:
<?php
$document = new DOMDocument();
$document->load( "s4133-0000000-203-fr.xml" );
$xpath = new DOMXpath($document);
foreach ($xpath->evaluate("//datas") as $datas)
$stadium = $xpath->evaluate("string(stadium/@name)", $datas);
$city = $xpath->evaluate("string(stadium/city/@name)", $datas);
$country = $xpath->evaluate("string(stadium/city/country/@name)", $datas);
$timestamp = $xpath->evaluate("string(parent::match/@timestamp)", $datas);
echo $timestamp."\n ".$stadium.", ".$city.", ".$country."\n";
?>
使用您的文件作为输入,将打印以下结果:
2014-07-12T17:00:00-03:00
Stade national Mané Garrincha, Brasilia, Brésil
2014-07-13T16:00:00-03:00
Stade Maracana, Rio de Janeiro, Brésil
2014-07-08T17:00:00-03:00
Estadio Mineirão, Belo Horizonte, Brésil
2014-07-09T17:00:00-03:00
Arena de São Paulo, Sao Paulo, Brésil
【讨论】:
使用 DOMXpath::evaluate():$stadium = $xpath->evaluate("string(stadium/@name)", $datas);
非常感谢@ThW。我不是真正的 PHP 程序员,所以我不太了解这些库 :) 有机会时我会编辑它(我现在正在通过手机浏览)。或者,您可以根据需要对其进行编辑。以上是关于从 XML 文件中获取节点在 php 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
if(isset($_FILES)) 在 PHP 中不起作用