PHP5 provides easy functions to access and manipulate xml.
SimpleXML and DOM.
SimpleXML sacrifices so many functionalities for simplicity.
This is a trade-off for the power and flexibility it provides.
You cannot remove and manipulate elements using simplexml directly.
But we can do the same using DOM. Also we can export simplexml objects to dom
and do the required modification. This is a pretty good feature.
Loading and Saving XML Documents:
There are two ways for loading xml documents.
First by loading from a file and second from string.
From File :
// PHP Script
<?php
$dom = new DOMDocument();
$dom->load("library.xml");
echo $dom->saveXML();
?>
//XML - Create a file library.xml and paste the following xml to it.
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
From string
// PHP Script
<?php
$xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
?>
Just as simply, you can save XML documents using one of DomDocument::save()
(to a file), DomDocument::saveXML() (to a string), DomDocument::saveHTML() (also
to a string, but saves an HTML document instead of an XML file), and
DomDocument:saveHTMLFile() (to a file in HTML format).
XPath Queries:
Some of the cream feature of DOM extension is its integration with XPath.
Actually DomXPath is far more powerful than its SimpleXML equivalent.
A call to DomXpath::query() will return a DomNodeList object; you can find out how
many items it contains by using the length property, and then access any one of them
with the item() method. You can also iterate through the entire collection using a
foreach() loop:
// PHP Script
<?php
$dom = new DomDocument();
$dom->load("library.xml");
$xpath = new DomXPath($dom);
$xpath->registerNamespace("lib","http://example.org/library");
$result = $xpath->query("//lib:title/text()");
foreach ($result as $book)
{
echo $book->data;
}
echo "<br/>";
echo "Total items : ".$result->length;
echo $result->length;
$book = $result->item (0);
echo $book->data;
?>
?>
// XML
<?xml version="1.0"?>
<library xmlns="http://example.org/library"
xmlns:meta="http://example.org/book-meta"
xmlns:pub="http://example.org/publisher"
xmlns:foo="http://example.org/foo">
<book meta:isbn="0345342968">
<title>Fahrenheit 451</title>
<author>Ray Bradbury</author>
<pub:publisher>Del Rey</pub:publisher>
</book>
</library>
Modifying XML Documents:
By using the DomDocument::createElement(), DomDocument::createElementNS(), and
DomDocument::createTextNode() methods we can modify an XML Document.
In the following example, we will add a
new book to our "libary.xml" document.
// PHP Script
<?php
$dom = new DomDocument();
$dom->load("library.xml");
$book = $dom->createElement("book");
$book->setAttribute("isbn","09784578");
$title = $dom->createElement("title");
$text = $dom->createTextNode("This is a text data");
$title->appendChild($text);
$book->appendChild($title);
$author = $dom->createElement("author","Author Name");
$book->appendChild($author);
$publisher = $dom->createElement("publisher", "Publisher Name");
$book->appendChild($publisher);
$dom->documentElement->appendChild($book);
echo $dom->saveXML();
?>
?>
Moving Data:
Moving Data is not as obvious as you might expect, because the DOM extension
doesnt provide a method that takes care of that, explicitly.
Instead, you can use a combination
of DomNode::appendChild() and DomNode::insertBefore().
Here, we take the second book element and place it before the first.
// PHP Script
<?php
$dom = new DomDocument();
$dom->load("library.xml");
$xpath = new DomXPath($dom);
// Before
echo "<pre>".$dom->saveXML()."</pre>";
$xpath->registerNamespace("lib","http://example.org/library");
$result = $xpath->query("//lib:book");
$result->item(1)->parentNode->insertBefore($result->item(1), $result->item(0));
// After
echo "<pre>".$dom->saveXML()."</pre>";
?>
?>
In the following example, on the other hand, we take the first book element and place it at the end:
// PHP Script
<?php
$dom = new DomDocument();
$dom->load("library.xml");
$xpath = new DomXPath($dom);
// Before
echo "<pre>".$dom->saveXML()."</pre>";
$xpath->registerNamespace("lib","http://example.org/library");
$result = $xpath->query("//lib:book");
$result->item(1)->parentNode->appendChild($result->item(0));
// After
echo "<pre>".$dom->saveXML()."</pre>";
?>
If you wish to duplicate a node, use "DomNode::cloneNode()" first:
// PHP Script
<?php
$dom = new DomDocument();
$dom->load("library.xml");
$xpath = new DomXPath($dom);
// Before
echo "<pre>".$dom->saveXML()."</pre>";
$xpath->registerNamespace("lib","http://example.org/library");
$result = $xpath->query("//lib:book");
$clone = $result->item(0)->cloneNode();
$result->item(1)->parentNode->appendChild($clone);
// After
echo "<pre>".$dom->saveXML()."</pre>";
?>
Modifying Data:
When modifying data, you typically want to edit the CDATA within a node. Apart
from using the methods shown above, you can use XPath to find a CDATA node and
modify its contents directly:
// PHP Script
<?php
$xml = <<<XML
<xml>
<text>some text here</text>
</xml>
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DomXpath($dom);
$node = $xpath->query("//text/text()")->item(0);
$node->data = ucwords($node->data);
echo $dom->saveXML();
?>
Removing Data:
DOM provides a different method for removing nodes, child and data.
DomNode::removeAttribute(), DomNode::removeChild() and
DomCharacterData::deleteData():
<?php
//echo "<pre>".$dom->saveXML()."</pre>";
$xml = <<<XML
<xml>
<text type="misc">some text here</text>
<text type="misc">some more text here</text>
<text type="misc">yet more text here</text>
</xml>
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DomXpath($dom);
$result = $xpath->query("//text");
$result->item(0)->parentNode->removeChild($result->item(0));
$result->item(1)->removeAttribute('type');
echo "<pre>".$dom->saveXML()."</pre>";
$result = $xpath->query('text()', $result->item(2));
$result->item(0)->deleteData(0, $result->item(0)->length);
echo "<pre>".$dom->saveXML()."</pre>";
?>
Interfacing with SimpleXML:
DOM is more than capable to handle namespaces on its own and, typically, you can,
for the most part, ignore them and pass attribute and element names with the appropriate
prefix directly to most DOM functions:
You can import SimpleXML objects for use with DOM by using
dom_import_simplexml():
And the reverse can be achieved using
simplexml_import_dom():
// PHP Script
<?php
$sxml = simplexml_load_file('library.xml');
$node = dom_import_simplexml($sxml);
$dom = new DomDocument();
$dom->importNode($node, true);
$dom->appendChild($node);
?>
// PHP Script
<?php
$dom = new DOMDocument();
$dom->load(’library.xml’);
$sxe = simplexml_import_dom($dom);
echo $sxe->book[0]->title;
?>
No comments:
Post a Comment