XML erzeugen: Unterschied zwischen den Versionen
Aus Wikizone
(Die Seite wurde neu angelegt: „== Siehe auch == SimpleXML“) |
|||
| (3 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
== Siehe auch == | == Siehe auch == | ||
[[SimpleXML]] | [[SimpleXML]] | ||
| + | |||
| + | == Mit dem DOM (Document Object Model) == | ||
| + | http://de3.php.net/manual/en/book.dom.php | ||
| + | === Beispiel === | ||
| + | https://stackoverflow.com/questions/143122/using-simplexml-to-create-an-xml-object-from-scratch | ||
| + | <syntaxhighlight lang="php"> | ||
| + | $domDoc = new DOMDocument; | ||
| + | $rootElt = $domDoc->createElement('root'); | ||
| + | $rootNode = $domDoc->appendChild($rootElt); | ||
| + | |||
| + | $subElt = $domDoc->createElement('foo'); | ||
| + | $attr = $domDoc->createAttribute('ah'); | ||
| + | $attrVal = $domDoc->createTextNode('OK'); | ||
| + | $attr->appendChild($attrVal); | ||
| + | $subElt->appendChild($attr); | ||
| + | $subNode = $rootNode->appendChild($subElt); | ||
| + | |||
| + | $textNode = $domDoc->createTextNode('Wow, it works!'); | ||
| + | $subNode->appendChild($textNode); | ||
| + | |||
| + | echo htmlentities($domDoc->saveXML()); | ||
| + | </syntaxhighlight> | ||
| + | Mit dem DOM sind recht flexible Manipulationen möglich: | ||
| + | $elem->append($doc->p('This is a paragraph')); | ||
| + | $elem->append('Some text'); | ||
| + | |||
| + | == Mit XMLWriter == | ||
| + | https://www.php.net/manual/en/intro.xmlwriter.php | ||
| + | https://www.php.net/manual/de/example.xmlwriter-simple.php | ||
| + | |||
| + | Soll einfacher sein als mit dem DOM Object | ||
| + | <syntaxhighlight lang="php"> | ||
| + | $w=new XMLWriter(); | ||
| + | $w->openMemory(); | ||
| + | $w->startDocument('1.0','UTF-8'); | ||
| + | $w->startElement("root"); | ||
| + | $w->writeAttribute("ah", "OK"); | ||
| + | $w->text('Wow, it works!'); | ||
| + | $w->endElement(); | ||
| + | echo htmlentities($w->outputMemory(true)); | ||
| + | </syntaxhighlight> | ||
Aktuelle Version vom 22. September 2021, 16:22 Uhr
Siehe auch[Bearbeiten]
SimpleXML
Mit dem DOM (Document Object Model)[Bearbeiten]
http://de3.php.net/manual/en/book.dom.php
Beispiel[Bearbeiten]
https://stackoverflow.com/questions/143122/using-simplexml-to-create-an-xml-object-from-scratch
$domDoc = new DOMDocument;
$rootElt = $domDoc->createElement('root');
$rootNode = $domDoc->appendChild($rootElt);
$subElt = $domDoc->createElement('foo');
$attr = $domDoc->createAttribute('ah');
$attrVal = $domDoc->createTextNode('OK');
$attr->appendChild($attrVal);
$subElt->appendChild($attr);
$subNode = $rootNode->appendChild($subElt);
$textNode = $domDoc->createTextNode('Wow, it works!');
$subNode->appendChild($textNode);
echo htmlentities($domDoc->saveXML());
Mit dem DOM sind recht flexible Manipulationen möglich:
$elem->append($doc->p('This is a paragraph'));
$elem->append('Some text');
Mit XMLWriter[Bearbeiten]
https://www.php.net/manual/en/intro.xmlwriter.php https://www.php.net/manual/de/example.xmlwriter-simple.php
Soll einfacher sein als mit dem DOM Object
$w=new XMLWriter();
$w->openMemory();
$w->startDocument('1.0','UTF-8');
$w->startElement("root");
$w->writeAttribute("ah", "OK");
$w->text('Wow, it works!');
$w->endElement();
echo htmlentities($w->outputMemory(true));