SimpleXML
Simple XML ist eine PHP Schnittstelle zum Parsen von XML Dateien.
Einführung
Generelles vorgehen
- Einlesen der Daten
- Entweder Parsen der kompletten Datei und Übertragen in array Struktur oder
- Zugriff auf bestimmtes Element. Meistens in zwei Schritten:
- xpath query
- Zugriff auf Element oder Attribute
SimpleXML 101
Quelle: http://stackoverflow.com/questions/1893024/basic-simplexml-working-example Zugriff 7/2013
Example:
<?xml version="1.0" encoding="ISO-8859-1"?> <programme> <title>Billy Bushwaka</title> <episodeNumber>2</episodeNumber> <description>Billy Bushwaka entertains</description> <url>play.swf</url> </programme>
First of all, always name your PHP variables after the node they represent.
// the root node is ie <programme/>
$programme = simplexml_load_file("local.xml");
Access to children (nodes) as if they were object properties.
echo $programme->title;
If there are multiple children using the same name, you can specify their 0-based position
// first <title/> child echo $programme->title[0];
// create or change the value of the second <title/> child $programme->title[1] = 'Second title';
Access to attributes as if they were array keys
// <mynode attr="attribute value" /> echo $mynode['attr'];
XPath always returns an array.
More Hints
Quick XML
$string = <<<XML <a> <foo name="one" game="lonely">1</foo> </a> XML;
Alternative xpath:
$result = $xml->xpath("//programme/title");
Quellcode der xml Datei ausgeben
echo $simplexml->asXML();
XML Daten von URL holen
$url = "http://username:password@url.com"; $xml = file_get_contents($url); $data = new SimpleXMLElement($xml);
Snippets
XML-Datei einlesen und in SimpleXML Objekt speichern
//einfache Variante
function fetch_xml_data($url){
$simplexml=simplexml_load_file(rawurlencode("https://".$this->user.":".$this->pw.'@'.$url));
$simplexml=simplexml_load_file("test.xml");
return $simplexml;
}
// Variante - Einlesen mit Context um diverse Header mit zu schicken
function fetch_xml_data_ctx($url){
// Erzeugen eines Streams
$headers = "Accept-language: de\r\n";
$headers .= "Accept: application/xml\r\n";
$headers .= "Authorization: Basic ".base64_encode("$this->user:$this->pw")."\r\n";
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=> $headers
)
);
$context = stream_context_create($opts);
// Öffnen der Datei mit den oben definierten HTTP-Headern
$file = file_get_contents('https://'.$url, false, $context);
$simplexml=simplexml_load_string($file);
//print_r($file);
return $simplexml;
}
Zugriff auf Werte
xPath
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');
while(list( , $node) = each($result)) {
echo '/a/b/c: ',$node,"\n";
}
/* Relative paths also work... */
$result = $xml->xpath('b/c');
while(list( , $node) = each($result)) {
echo 'b/c: ',$node,"\n";
}
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
/a/b/c: text /a/b/c: stuff b/c: text b/c: stuff
Zugriff auf Attribute
foreach( $sxe->attributes() as $attr_name => $attr_value ){
$arr['attributes'][(string)$attr_name] = (string)$attr_value;
}
Hinweis: Damit der Wert des Attributes als String ausgegeben wird muß man Casten (string) ansonsten wird ein Objekt zurück gegeben.
Kindknoten abfragen
$children = $sxe->children($ns,TRUE);
$ns ist der Namespace, der zweite Parameter sagt ob rekursiv gesucht wird.
Namespaces registrieren
Über eine kleine Funktion kann man sich arbeit sparen.
function register_namespaces($sxe){
$arrNs = $sxe->getNamespaces(true);
foreach($arrNs as $prefix => $ns){
$sxe->registerXPathNamespace($prefix, $ns);
echo($prefix.' = '.$ns);
}
}
Alle im XML Dokument angegebenen Namespaces werden geparsed und Registriert. So kann man sie in Kurzform ansprechen.
SimpleXML und Namespaces
Allgemein
$xml = <<<EOD
<book xmlns:chap="http://example.org/chapter-title">
<title>My Book</title>
<chapter id="1">
<chap:title>Chapter 1</chap:title>
<para>Donec velit. ...</para>
</chapter>
<chapter id="2">
<chap:title>Chapter 2</chap:title>
<para>Lorem ipsum ...</para>
</chapter>
</book>
EOD;
$sxe = new SimpleXMLElement($xml);
// Zugriff mit Namespace Prefix
$sxe->registerXPathNamespace('c', 'http://example.org/chapter-title');
$result = $sxe->xpath('//c:title');
foreach ($result as $title) {
echo $title . "\n";
}
Direkter Zugriff im nächsen Beispiel
Beispiel Ebay Timestamp
$response = <<< XMLBLOCK
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<GeteBayOfficialTimeResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2005-10-28T01:01:04.668Z</Timestamp>
<Ack>Success</Ack>
<Version>429</Version>
<Build>e429_intl_Bundled_1949355_R1</Build>
</GeteBayOfficialTimeResponse>
</soapenv:Body>
</soapenv:Envelope>
XMLBLOCK;
$xml = simplexml_load_string($response);
Zugriff auf Timestamp:
echo "Time: " .
$xml->children('http://schemas.xmlsoap.org/soap/envelope/')->children('urn:ebay:apis:eBLBaseComponents')->GeteBayOfficialTimeResponse->Timestamp . "\n";
oder
$xml->children('soapenv', true)->children()->GeteBayOfficialTimeResponse->Timestamp