SimpleXML: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 59: Zeile 59:
 
$xml = file_get_contents($url);
 
$xml = file_get_contents($url);
 
$data = new SimpleXMLElement($xml);
 
$data = new SimpleXMLElement($xml);
 +
</pre>
 +
 +
== SimpleXML und Namespaces ==
 +
<pre>
 +
$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);
 +
</pre>
 +
 +
Zugriff auf Timestamp:
 +
<pre>
 +
echo "Time: " .
 +
$xml->children('http://schemas.xmlsoap.org/soap/envelope/')->children('urn:ebay:apis:eBLBaseComponents')->GeteBayOfficialTimeResponse->Timestamp . "\n";
 +
</pre>
 +
 +
oder
 +
 +
<pre>
 +
$xml->children('soapenv', true)->children()->GeteBayOfficialTimeResponse->Timestamp
 +
</pre>
 
</pre>
 
</pre>

Version vom 4. Juli 2013, 13:23 Uhr

Simple XML ist eine PHP Schnittstelle zum Parsen von XML Dateien.

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

SimpleXML und Namespaces

$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