Search
Close this search box.
Search
Close this search box.

Parsing XML With The DOM Library

Although DOM can be intimidating, I suggest that you learn using it, instead of using SimpleXML. Believe me, it’s very powerful and you won’t regret it.

Here is a typical XML (let’s name it my_xml.xml), with which we will work:

<?xml version="1.0" encoding="UTF-8"?>
<index>
	<books>

		<book id='0'>
			<title>Thus Spoke Zarathustra</title>
			<author>Friedrich Nietzsche</author>
		</book>

		<book id='1'>
			<title>The Stranger</title>
			<author>Albert Camus</author>
		</book>

		<book id='2'>
			<title>The Idiot</title>
			<author>Fyodor Dostoyevsky/author>
		</book>

		<book id='3'>
			<title>Faust</title>
			<author>Goethe</author>
		</book>

	</books>

	<movies>

		<movie>

		</movie>

	</movies>

</index>

Loading XMLDocuments

The DOM extension provides an easy way to load xml documents by filename:

$source = 'my_xml.xml';
$dom = new DomDocument();
$dom->load($source);

Retaining user-friendy format (with linespaces, indents, etc.)

$source = 'my_xml.xml';
$dom = new DomDocument();
$dom->formatOutput = true;
$dom->load($source, LIBXML_NOBLANKS);

Echoing an DOM

While debugging, you may want to echo the DOM, before saving it to a file, just to make sure that everything works as it should.

print "<textarea cols='500' rows='200'>".htmlspecialchars($dom->saveXML())."</textarea>";

XPath Queries

One of the most powerful features of the DOM extension is the way in which it integrates with XPath queries.

Say that you want to return the titles of all the books.

$xp = new domxpath($dom);
$result = $xp->query("//books/book/title");
foreach ($result as $title) { // name the title var anything you want
    echo $title->nodeValue . "<br/>";
}

The output is:

Thus Spoke Zarathustra
The Stranger
The Idiot
Faust

If you want to get the id attributes from book nodes, then:

$result = $xp->query("//books/book");
foreach ($result as $book) { // name the title var anything you want
    echo $book->getAttribute('id')."";
}

The output is:

0
1
2
3

Or if you want to get the author of the second book, then:

$result = $xp->query("//books/book/author");
print $result->item(1)->nodeValue;

Using vars with xPath

Just use double quotation marks:

$counter = 2;
$result = $xp->query("//books/book[$counter]/title");
print $result->item(0)->nodeValue;

Adding new nodes

To enter a new book:

// crete new book node
$book = $dom->createElement('book');

// set year attribute
$book->setAttribute('id', '4');

// create title sub-node
$title= $dom->createElement('title');
$book->appendChild($title);  

# create author sub-node
$author = $dom->createElement('author');
$book->appendChild($author);

# create a cdata node
$cdataNode = $dom->createElement('cdataNodeTitle');
$cdata = $dom->createCDataSection('cdata content');
$cdataNode ->appendChild($cdata);
$book->appendChild($cdataNode);

# add new book to the end of the queue
$books = $xp->query("//books");
$books->item(0)->appendChild($book );

To enter the new book to the top of the queue use:

$result = $xp->query('//books/book');
$result->item(0)->parentNode->insertBefore($book,$result->item(0));

Modifying a node value

To change the text of the second book, use the nodeValue function:

$result = $xp->query("//books/book[1]");
$result->item(0)->nodeValue = 'The fall';

Modifying an attribute

To change the id of the second book, use the setAttribute function:

$result = $xp->query("//books/book[1]");
$result->item(0)->setAttribute('id', '14');

Moving Data

dafas

Removing Data From XML Documents

To remove the first book:

$xp = new domxpath($dom);
$result = $xp->query("//books/book");
$result->item(0)->parentNode->removeChild($result->item(0));

Saving the modified XML Document

Having finished modifying the DOM, you ‘ll want to save it.

$dom->save($source);

Learning Resources

  • Parsing XML with the Dom Library tutorial.
  • XML create, add, edit, modify using DOM, xpath tutorial.