PHP XML
Create a XML using DomDocument
To create a XML using DOMDocument,basically, we need to create all the tags and attributes using the createElement() and createAttribute() methods and them create the XML structure with the appendChild().
The example below includes tags, attributes, a CDATA section and a different namespace for the second tag:
$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
//create the main tags, without values
$books = $dom->createElement('books');
$book_1 = $dom->createElement('book');
// create some tags with values
$name_1 = $dom->createElement('name', 'PHP - An Introduction');
$price_1 = $dom->createElement('price', '$5.95');
$id_1 = $dom->createElement('id', '1');
//create and append an attribute
$attr_1 = $dom->createAttribute('version');
$attr_1->value = '1.0';
//append the attribute
$id_1->appendChild($attr_1);
//create the second tag book with different namespace
$namespace = 'www.example.com/libraryns/1.0';
//include the namespace prefix in the books tag
$books->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ns', $namespace);
$book_2 = $dom->createElementNS($namespace,'ns:book');
$name_2 = $dom->createElementNS($namespace, 'ns:name');
//create a CDATA section (that is another DOMNode instance) and put it inside the name tag
$name_cdata = $dom->createCDATASection('PHP - Advanced');
$name_2->appendChild($name_cdata);
$price_2 = $dom->createElementNS($namespace, 'ns:price', '$25.00');
$id_2 = $dom->createElementNS($namespace, 'ns:id', '2');
//create the XML structure
$books->appendChild($book_1);
$book_1->appendChild($name_1);
$book_1->appendChild($price_1);
$book_1->appendChild($id_1);
$books->appendChild($book_2);
$book_2->appendChild($name_2);
$book_2->appendChild($price_2);
$book_2->appendChild($id_2);
$dom->appendChild($books);
//saveXML() method returns the XML in a String
print_r ($dom->saveXML());
This will output the following XML:
<?xml version="1.0" encoding="utf-8"?>
<books xmlns:ns="www.example.com/libraryns/1.0">
<book>
<name>PHP - An Introduction</name>
<price>$5.95</price>
<id version="1.0">1</id>
</book>
<ns:book>
<ns:name><![CDATA[PHP - Advanced]]></ns:name>
<ns:price>$25.00</ns:price>
<ns:id>2</ns:id>
</ns:book>
</books>
Read a XML document with DOMDocument
Similarly to the SimpleXML, you can use DOMDocument to parse XML from a string or from a XML file
1. From a string
$doc = new DOMDocument();
$doc->loadXML($string);
2. From a file
$doc = new DOMDocument();
$doc->load('books.xml');// use the actual file path. Absolute or relative
Example of parsing
Considering the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<name>PHP - An Introduction</name>
<price>$5.95</price>
<id>1</id>
</book>
<book>
<name>PHP - Advanced</name>
<price>$25.00</price>
<id>2</id>
</book>
</books>
This is a example code to parse it
$books = $doc->getElementsByTagName('book');
foreach ($books as $book) {
$title = $book->getElementsByTagName('name')->item(0)->nodeValue;
$price = $book->getElementsByTagName('price')->item(0)->nodeValue;
$id = $book->getElementsByTagName('id')->item(0)->nodeValue;
print_r ("The title of the book $id is $title and it costs $price." . "\n");
}
Output
The title of the book 1 is PHP - An Introduction and it costs $5.95.
The title of the book 2 is PHP - Advanced and it costs $25.00.
The title of the book 2 is PHP - Advanced and it costs $25.00.
Create an XML file using XMLWriter
Instantiate a XMLWriter object:
$xml = new XMLWriter();
Next open the file to which you want to write. For example, to write to /var/www/example.com/xml/output.xml,
use:
$xml->openUri('file:///var/www/example.com/xml/output.xml');
To start the document (create the XML open tag):
$xml->startDocument('1.0', 'utf-8');
This will output:
<?xml version="1.0" encoding="UTF-8"?>
Now you can start writing elements:
$xml->writeElement('foo', 'bar');
This will generate the XML:
<foo>bar</foo>
If you need something a little more complex than simply nodes with plain values, you can also "start" an element and add attributes to it before closing it:
$xml->startElement('foo');
$xml->writeAttribute('bar', 'baz');
$xml->writeCdata('Lorem ipsum');
$xml->endElement();
This will output:
<foo bar="baz"><![CDATA[Lorem ipsum]]></foo>
Read a XML document with SimpleXML
You can parse XML from a string or from a XML file
1. From a string
$xml_obj = simplexml_load_string($string);
2. From a file
$xml_obj = simplexml_load_file('books.xml');
Example of parsing
Considering the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<name>PHP - An Introduction</name>
<price>$5.95</price>
<id>1</id>
</book>
<book>
<name>PHP - Advanced</name>
<price>$25.00</price>
<id>2</id>
</book>
</books>
This is a example code to parse it
$xml = simplexml_load_string($xml_string);
$books = $xml->book;
foreach ($books as $book) {
$id = $book->id;
$title = $book->name;
$price = $book->price;
print_r ("The title of the book $id is $title and it costs $price." . "\n");
}
This will output:
Output
The title of the book 1 is PHP - An Introduction and it costs $5.95.
The title of the book 2 is PHP - Advanced and it costs $25.00.
The title of the book 2 is PHP - Advanced and it costs $25.00.