Background
With XML Parser, you use can only read XML. To write and otherwise manipulate an XML document, you can use PHP DOM Extension. DOM stands for Document Object Model and it is a way of expressing XML nodes as a hierarchical tree of objects.
[wp_ad_camp_1]
An XML document is read into memory as a tree of objects. Unlike with the XML Parser extension, the PHP DOM extension can traverse the tree in any directions to explore various elements, attributes, text nodes, and other nodes in the XML. Nodes can be modified too.
Software Requirements
- Windows 7 Professional SP1
- PHP 5.5.9 / Zend Engine v2.5.0
- NetBeans IDE 8.0.1
- ApacheFriends XAMPP Version 1.8.3
- Apache/2.4.7 (Win32) / Open SSL/1.0.1e
DOM Classes
Using DOM involves working with several built-in classes. The most common ones are the following:
- DOMNode
This represents a single node in the DOM tree. Most DOM classes derive from DOMNode class. - DOMDocument
This represents the entire XML document in the form of a DOM tree stored in the memory. It derives from DMNode class, and is, the root of the tree. - DOMElement
This represents an element node. - DOMAttr
This represents an element’s attribute. - DOMText
This represents a plain-text node.
[wp_ad_camp_4]
- DOMCharacterData
This represents a CDATA (character data) node.
Working with DOM
Reading XML
The basic steps for reading XML documents are as follow:
- Create a DOMDocument object1$payment_transaction = new DOMDocument();
- Read an XML String or load an XML file12345// Read an XML String$payment_transaction->loadXML($XML_string);// load XML from file$payment_transaction->loadXML('trx01.xml');
- Traverse the content of the DOMDocument object using DOM-related properties and functions12345678910111213141516171819202122232425262728293031323334function displayXMLDocument($node) {switch ( $node->nodeType ) {case XML_ELEMENT_NODE:echo "Found element: $node->tagName";if($node->hasAttributes()) {echo " with attributes: \n";}foreach($node->attributes as $attribute ) {echo "$attribute->name = $attribute->value\n";}break;case XML_CDATA_SECTION_NODE:if ( trim($node->data) ) {echo "Found character data node: " . htmlspecialchars($node->data) . "\n";}break;case XML_TEXT_NODE:if(trim($node->wholeText)) {echo "Found text node: $node->wholeText\n";}break;}if($node->hasChildNodes()) {foreach ($node->childNodes as $child ) {displayXMLDocument($child);}}}// Pass the DOMDocument object to this user-defined functiondisplayXMLDocument($payment_transaction);
Creating an XML Document
The basic steps for creating an XML document are as follow:
[wp_ad_camp_5]
- Create an object of DOMDocument12$new_payment_transaction = new DOMDocument("1.0", "UTF-8");$new_payment_transaction ->formatOutput = true;
- Build up the document1234567891011121314$new_payment_transaction = new DOMDocument("1.0", "UTF-8");$new_payment_transaction->formatOutput = true;// Create the root "payment" element$payment_root = $new_payment_transaction->createElement("payment");$new_payment_transaction->appendChild($payment_root);// Create the first "account"$account = $new_payment_transaction->createElement("account");$account->setAttribute("id", "0123456789");$account->setAttribute("type", "nonmember");$payment_root->appendChild($account);// Create the account’s "payment-details" child element$payment_details = $new_payment_transaction->createElement("amount", "100");$account->appendChild( $payment_details );
- Save or print the content of the XML document12345echo $new_payment_transaction->saveXML($new_payment_transaction->documentElement);// or save to file$new_payment_transaction->save('new_payment.xml');
Sample Output
1 2 3 4 5 | <payment> <account id="0123456789" type="nonmember"> <amount>100</amount> </account> </payment> |