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

2 Answers 2

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;

}


?>
Sign up to request clarification or add additional context in comments.

10 Comments

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.
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) }
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
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
thank you, please just remove the acutal url from your answer, I'll give it a try.
|
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.