How to use PHPDOM to create dynamic XML files

Source: Internet
Author: User
Tags data structures

When processing XML-based applications, developers often need to establish XML-encoded data structures. For example, in the Web, XML status templates based on user input, XML statements for server requests, and customer responses based on runtime parameters.

Although the construction of XML data structures is time-consuming, everything becomes simple and clear if mature php dom application interfaces are used. This article will introduce you to the main functions of the php dom application interface and demonstrate how to generate a correct complete XML file and save it to the disk.

Create document type declaration

Generally, the XML declaration is placed at the top of the document. The declaration in PHP is very simple: you only need to instantiate an object in the DOM document class and assign it a version number. View program List:

Program List

<? Php
// Create doctype
$ Dom = new DOMDocument ("1.0 ");

// Display document in browser as plain text
// Display document in browser as plain text
// For readability purposes
Header ("Content-Type: text/plain ");

// Save and display tree
Echo $ dom-> saveXML ();
?>

Pay attention to the saveXML () method of DOM document objects. I will introduce this method later. Now you just need to familiarize yourself with the method used to output the current snapshot of the XML document to a file or browser. In this example, to enhance readability, I have output the ASCII code text directly to the browser. In practical applications, you can send text/XML header files to the browser.

For example, view the output in the browser, you can see the following code:

<? Xml version = "1.0"?>

Add element and text nodes

The real powerful feature of XML is its element and encapsulated content. Fortunately, once you initialize the DOM document, many operations become simple. This process includes the following two steps:

The createElement () or createTextNode () method of the DOM document object is called for each element or text node you want to add. This creates a new object corresponding to an element or a text node.
Call the appendChild () method of the node, pass it to the object created in the previous step, and add the element or text node to the parent node in the XML document tree.
The following example clearly demonstrates the two steps. Check program List B.

Program List B

<? Php
// Create doctype
$ Dom = new DOMDocument ("1.0 ");

// Display document in browser as plain text
// For readability purposes
Header ("Content-Type: text/plain ");

// Create root element
$ Root = $ dom-> createElement ("toppings ");
$ Dom-> appendChild ($ root );

// Create child element
$ Item = $ dom-> createElement ("item ");
$ Root-> appendChild ($ item );

// Create text node
$ Text = $ dom-> createTextNode ("pepperoni ");
$ Item-> appendChild ($ text );

// Save and display tree
Echo $ dom-> saveXML ();
?>

Here, I first create a root element named <toppings> and add it to the XML header file. Then, I create an element named <item> and assign it to the root element. Finally, I create a text node with the value "pepperoni" and assign it to the <item> element. The final result is as follows:

<? Xml version = "1.0"?>
<Toppings>

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.