在php中循环遍历XML-Object

时间:2015-02-23 14:51:12

标签: php xml loops

我试图像这样循环一个xml对象......

$xml_url = "http://somdomain.com/frieventexport.php";

$curl = curl_init();
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_URL, $xml_url );

$xml = curl_exec( $curl );
curl_close( $curl );

$document = new DOMDocument;
$document->loadXML( $xml );

var_dump($document);
//echo $document->childNodes[1];

var_dump我得到了这个。

object(DOMDocument)#1 (34) { ["doctype"]=> NULL ["implementation"]=> string(22) "(object value omitted)" ["documentElement"]=> NULL ["actualEncoding"]=> NULL ["encoding"]=> NULL ["xmlEncoding"]=> NULL ["standalone"]=> bool(true) ["xmlStandalone"]=> bool(true) ["version"]=> string(3) "1.0" ["xmlVersion"]=> string(3) "1.0" ["strictErrorChecking"]=> bool(true) ["documentURI"]=> NULL ["config"]=> NULL ["formatOutput"]=> bool(false) ["validateOnParse"]=> bool(false) ["resolveExternals"]=> bool(false) ["preserveWhiteSpace"]=> bool(true) ["recover"]=> bool(false) ["substituteEntities"]=> bool(false) ["nodeName"]=> string(9) "#document" ["nodeValue"]=> NULL ["nodeType"]=> int(9) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["attributes"]=> NULL ["ownerDocument"]=> NULL ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> NULL ["baseURI"]=> NULL ["textContent"]=> string(0) "" }

是否有人知道如何从中提取信息并使用单个xml-items?

这将在xml:

<event>
<titel>Eventtitle</titel>
<datum>07.03.2015</datum>
<von>18:30</von>
<bis>23:00</bis>
<ort>Something</ort>
<referent>BezRKdo</referent>
<veranstalter>Who does it.</veranstalter>
</event>

我不知道如何遍历此转储并提取信息。 亲切的问候, 马特

2 个答案:

答案 0 :(得分:1)

<?php

$xml_url = "http://localhost/site.xml";

$curl = curl_init();
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_URL, $xml_url );

$xml = curl_exec( $curl );
curl_close( $curl );

$xml = iconv('UTF-8', 'UTF-8//IGNORE', $xml);

$document = new DOMDocument;
$document->loadXML( $xml );

$events = $document->getElementsByTagName("event");

foreach( $events as $event ){
    $titels = $event->getElementsByTagName( "titel" );
    $titel= $titels->item(0)->nodeValue;

    $datums = $event->getElementsByTagName( "datum" );
    $datum = $datums->item(0)->nodeValue;

    echo $titel;

}


?>

答案 1 :(得分:0)

XML中只有一条记录,每个数据元素都是唯一的。您可以使用XPath将数据作为字符串获取。

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);

echo $xpath->evaluate('string(/event/titel)'), "\n";
echo $xpath->evaluate('string(/event/datum)'), ' ';
echo $xpath->evaluate('string(/event/von)'), '-';
echo $xpath->evaluate('string(/event/bis)');

输出:

Übung Einhausung Amras
07.03.2015 18:30-23:00

如果您的XML将返回多个事件,则需要循环事件元素并将该节点用作上下文:

多个元素的XML结构:

<events>
  <event>
    <titel>Übung Einhausung Amras</titel>
    ...
  </event>
  <event>
    <titel>Other event</titel>
    ...
  </event>
  ...
</events>

PHP迭代XML中的多个记录:

foreach($xpath->evaluate('/events/event' as $event) {
  echo $xpath->evaluate('string(/event/titel)', $event), "\n";
  // ...
}

编码

响应中的XML声明缺少编码。它是<?xml version="1.0"?>,但应该是<?xml version="1.0" encoding="ISO-8859-1"?>

DOMDocument默认使用UTF-8。您可以转换字符串或确保XML定义编码。

$xml = iconv('ISO-8859-1', 'UTF-8//IGNORE', $xml);

由于使用了charset,utf8_encode()也应该有效:

$xml = utf8_encode($xml);