SimpleXML: Unterschied zwischen den Versionen
Aus Wikizone
(Die Seite wurde neu angelegt: „Simple XML ist eine PHP Schnittstelle zum Parsen von XML Dateien. == XML Daten von URL holen == <pre> $url = "http://username:password@url.com"; $xml = file_get_…“) |
|||
| Zeile 1: | Zeile 1: | ||
Simple XML ist eine PHP Schnittstelle zum Parsen von XML Dateien. | 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 | ||
| + | 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. | ||
== XML Daten von URL holen == | == XML Daten von URL holen == | ||
Version vom 4. Juli 2013, 12:22 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 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.
XML Daten von URL holen
$url = "http://username:password@url.com"; $xml = file_get_contents($url); $data = new SimpleXMLElement($xml);