Php method for creating XML documents based on DOM

Source: Internet
Author: User
This article mainly introduces the PHP method of creating XML documents based on DOM, and analyzes the related operation skills of PHP using DOM to create XML format files, which can be referenced by friends.

The example in this article describes how PHP creates XML documents based on the DOM. Share to everyone for your reference, as follows:

DOM Create XML document

Create the following document with the DOM:

<booklist> <book id= "1" > <title> tianlong eight </title> <author> Jin Yong </author> <content > <! [Cdata[Tianlong Eight is a novel written by Jin Yong, very good-looking! ]]> </content> </book></booklist>

Implementation steps:

1, create the DOM object--"2, create a node--" 3, create a subordinate node--"4, add the subordinate node to the ancestor node--" 5, create the attribute node--"6, add the attribute node to the node that owns the attribute--" 7, if there is a node repeat 2~6 step--"8, Add the highest node (that is, the root node) to the DOM object--"9, open or store the XML document.

The process of creating a node can either be created from the most subordinate node or start from the root node. The implementation code is as follows:

<?phpheader (' content-type:text/xml; '); $dom = new DOMDocument (' 1.0 ', ' utf-8 ');//Build DOM Object $no1 = $dom->createelement (' Booklist ');//Create normal node:booklist$dom-> AppendChild ($no 1);//Add the Booklist node to the DOM document $NO2 = $dom->createelement (' book ');//Create Book node $no1->appendchild ($ NO2);//Add the book node to the Booklist node $no3 = $dom->createattribute (' id ');//Create attribute node: Id$no3->value = 1;//Assign value to attribute node $no2-> AppendChild ($no 3);//Add the attribute node to the book node $no3 = $dom->createelement (' title '), $no 2->appendchild ($no 3); $no 4 = $dom- >createtextnode (' Tianlong Eight ');//Create Text node: Tianlong eight $no3->appendchild ($no 4);//Add Tianlong eight nodes to the book node $NO3 = $dom createelement (' author '); $no 2->appendchild ($no 3); $no 4 = $dom->createtextnode (' Jin Yong ');//Create Text node: Tianlong eight $no3-> AppendChild ($no 4);//Add the Tianlong eight node to the book node $no3 = $dom->createelement (' content '); $no 2->appendchild ($no 3); $no 4 = $ Dom->createcdatasection (' Tian Long Eight is a martial arts fiction written by Jin Yong, very good-looking! ');//Create the text CDATA node $no3->appendchild ($no 4);//Add the Tianlong eight nodes to the book Node header (' Content-type:text/html;charset=utf-8 '); echo $dom->save (' bookList.xml ')? ' Storage succeeded ': ' Storage failed ';//stored as XML document/* Directly open header (' Content-type:text/xml ') in XML document format; echo $dom->savexml ();*/?>

The above is the whole content of this article, I hope that everyone's study has helped.


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.