PHP Beginner's Introduction to XML and DOM
1.What is DOM
W3C DOM provides a standard set of objects for HTML and XML documents, as well as standards for accessing and manipulating these documents interface.
W3C DOM is divided into different parts (Core, XML and HTML) and different levels (DOM Level 1/2/3):
* Core DOM - A standard set of objects that defines a standard for any structured document
* XML DOM -The Object Collection of the Standard of the XML Document
* HTML DOM -is the Object Collection of the HTML Document Definition Standard
# If you need to read and update-create and process-a XML document, you need XML parser. There are two basic XML parser types:
· Tree -based parser: This parser converts the XML document into a tree structure. It analyzes the entire document and provides access to elements in the tree, such as the Document Object Model (DOM). · Time-based parser: Treat XML documents as a series of events. When a specific event occurs, the parser calls a function to handle it. The DOM parser is a tree-based parserLook at the xml document fragment below<?xml version="1.0" encoding="ISO-8859 -1"?><from>Jani</from>
XML DOM Treat the above XML as a tree structure:Level 2: Root element: <from>
Level 3: Text element: "Jani"Example:
First we create an xml file head.xml with the following code<?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>Then we create a php file with the following code
<?php $xmlDoc = new DOMDocument(); $xmlDoc->load("head.xml"); print $xmlDoc->saveXML(); ?>
How to traverse xml
Create a xml file first, for Head.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>and then create a PHP file.