PHP XML DOM

Source: Internet
Author: User
Tags xml parser

PhpXML DOM

The built-in DOM parser makes it possible to work with XML documents in PHP.

What is DOM?

The DOM provides a standard set of objects for HTML and XML documents, as well as a standard interface for accessing and manipulating these documents.

The DOM is divided into different sections (Core, XML and HTML) and different levels (DOM level 1/2/3):

* Core DOM-defines a standard set of objects for any structured document
* XML DOM-defines a standard set of objects for an XML document
* HTML DOM-defines a standard set of objects for HTML documents

To learn more about the XML DOM, please visit our XML DOM tutorials.

XML parsing

To read and update-Create and process-an XML document, you need an XML parser.

There are two basic types of XML parsers:

    • Tree-based parser: This parser transforms an XML document into a tree structure. It parses the entire document and provides access to the elements in the tree, such as the Document Object Model (DOM).
    • Time-based parser: treats an XML document as a series of events. When a specific event occurs, the parser invokes the function to process it.

The DOM parser is a tree-based parser.

Take a look at the following XML document fragment:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<from>Jani</from>

The XML DOM treats the XML above as a tree structure:

    • Level 1:xml Documentation
    • Level 2: root element: <from>
    • Level 3: Text element: "Jani"
Installation

DOM XML parser functions are part of the PHP core. These functions can be used without installation.

XML file

The following XML file will be applied to our instance:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<body>don ' t forget me this weekend!</body>
</note>

Loading and exporting XML

We need to initialize the XML parser, load the XML, and output it:

Example<?php
$xmlDoc = new DOMDocument ();
$xmlDoc->load ("Note.xml");

Print $xmlDoc->savexml ();
?>

The above code will output:

Tovejanireminder Don ' t forget me this weekend!

If you view the source code in a browser window, you will see the following HTML:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<body>don ' t forget me this weekend!</body>
</note>

The above example creates a domdocument-object and loads the XML from "Note.xml" into the Document object.

The SaveXML () function puts an internal XML document into a string so that we can output it.

Traversing XML

We want to initialize the XML parser, load the XML, and traverse all elements of the <note> element:

Example<?php
$xmlDoc = new DOMDocument ();
$xmlDoc->load ("Note.xml");

$x = $xmlDoc->documentelement;
foreach ($x->childnodes as $item)
{
Print $item->nodename. " = " . $item->nodevalue. "<br>";
}
?>

The above code will output:

#text =
to = Tove
#text =
From = Jani
#text =
Heading = Reminder
#text =
BODY = Don ' t forget me this weekend!
#text =

In the example above, you see that there is an empty text node between each element.

When XML is generated, it typically contains whitespace between nodes. The XML DOM parser treats them as normal elements, which can sometimes cause problems if you don't pay attention to them.

To learn more about the XML DOM, please visit our XML DOM tutorials.

PHP XML DOM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.