0

i'm trying to loop through an xml object like this …

$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];

With var_dump I get this.

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) "" }

does anybody know how to extract information out of this and use the single xml-items?

this would be in the 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>

I don't know how to loop through this dump and extract the information. Kind Regards, Matt

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
matt
  • 42,713
  • 103
  • 264
  • 397

2 Answers2

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

}


?>
Mustafa Toker
  • 344
  • 3
  • 13
  • Thank you, but the XML comes with this domain above and this should be dynamic. So the `$xml` variable you point out, does not exist in this way. See my var_dump() output. If I `echo $titel` within the foreach loop i get no output. – matt Feb 23 '15 at 15:07
  • Also if I just put `$events = $document->getElementsByTagName("event"); var_dump($events);` outside the loop I get this `object(DOMNodeList)#2 (1) { ["length"]=> int(0) }` – matt Feb 23 '15 at 15:10
  • I gave the example of the XML variable. You must change the xml you fetching from the remote site. if you give the URL is not special, i can help you – Mustafa Toker Feb 23 '15 at 15:11
  • doesnt work because xml give a error "Warning: DOMDocument::loadXML() [domdocument.loadxml]: Input is not proper UTF-8, indicate encoding ! Bytes: 0xDC 0x62 0x75 0x6E in Entity, line: 3". i fixed, put your code $xml = iconv('UTF-8', 'UTF-8//IGNORE', $xml); and i'm updated my answer above – Mustafa Toker Feb 23 '15 at 15:22
  • thank you, please just remove the acutal url from your answer, I'll give it a try. – matt Feb 23 '15 at 15:27
  • thank you, seems to work except for the "Umlauts" in the Titel, those are neglected. It says "bung" in the output instead of "Übung" in the actual xml. – matt Feb 23 '15 at 15:29
  • Thank you, only last problem are the "Umlauts". – matt Feb 23 '15 at 15:35
  • does not give me the error, you can take screenshots? and look here : http://stackoverflow.com/questions/15500313/php-replace-umlaut-characters-to-parse-xml-several-methods-not-working – Mustafa Toker Feb 23 '15 at 15:58
  • hey @MustafaToker no, does not work for me … https://www.dropbox.com/s/m3a0znt8boxpx0c/xml-test.php?dl=0 I still get no Umlauts, what could cause this? Any ideas? – matt Feb 24 '15 at 10:01
  • use $xml = utf8_encode($xml); instead of $xml = iconv('UTF-8', 'UTF-8//IGNORE', $xml); – Mustafa Toker Feb 24 '15 at 13:18
0

You have only a single record in the XML, each data element is unique. You can use XPath to fetch the data as strings.

$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)');

Output:

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

If your XML would return multiple events you would need to loop the event elements and use the node as context:

XML structure for multiple elements:

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

PHP iterating over multiple records in XML:

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

Encoding

The XML declaration in the response is missing the encoding. It is <?xml version="1.0"?>, but it should be something like <?xml version="1.0" encoding="ISO-8859-1"?>.

DOMDocument uses UTF-8 by default. You can convert the string or make sure that the XML defines the encoding.

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

Because of the used charset, utf8_encode() should work, too:

$xml = utf8_encode($xml);
ThW
  • 19,120
  • 3
  • 22
  • 44