Create and parse xml programs using DOMDocument and SimpleXML in php

Source: Internet
Author: User
Tags php book
Create a DOMDocument object using DOM extension, add elements to the document using the DOMDocument object method, and generate the document using the save () or saveXML () method, you can also use SimpleXML to quickly solve the problem... create a DOMDocument object using DOM extension, add elements to the document using the DOMDocument object method, and generate the document using the save () or saveXML () method, you can also use SimpleXML to quickly parse xml documents.

Example: Dom xml parser functions are a core component of PHP and can be used without installation.

The following XML file will be used in our example. the code is as follows:

  
 
  George
  
 
  John
  
 
  Reminder
  Don't forget the meeting!  

Load and output XML, We need to initialize the XML parser, load the XML, and output it. The example code is as follows:

 Load ("note. xml"); print $ xmlDoc-> saveXML (); // output of the above code: // George John Reminder Don't forget the meeting

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

  
 
  George
  
 
  John
  
 
  Reminder
  Don't forget the meeting!  

In the preceding example, a DOMDocument-Object is created and the xml in "note. XML" is loaded into this document Object.

The saveXML () function puts the internal XML document into a string so that we can output It. the example code is as follows:

 AppendChild ($ dom-> createElement ('book'); // create a title element, and add it to $ book $ title = $ book-> appendChild ($ dom-> createElement ('title ')); $ title-> appendChild ($ dom-> createTextNode ('php book'); // add a property cover for the title element, attribute value soft $ title-> setAttribute ('cover', 'Soft'); // creates an author element, and add it to $ book $ kaifabu = $ book-> appendChild ($ dom-> createElement ('author ')); // add the text $ kaifabu-> appendChild ($ dom-> createTextNode ('kaifabu ') to the author element; // Create The author element, and add it to $ book $ kaifabu = $ book-> appendChild ($ dom-> createElement ('author ')); // add the text $ kaifabu-> appendChild ($ dom-> createTextNode ('yunker') to the author element ')); // output the formatted DOM file as XML $ dom-> formatOutput = true; // Generate books. xml file $ dom-> save ('books. XML'); // This step displays the generated xml in string form echo htmlentities ($ dom-> saveXML (); // The output result is as follows: // open source code phprm.com
    Php BookKaifabuYounker  

Note: If the XML generated above is changed to $ dom-> save ('books. XML'), a books. xml file is directly generated.

Now we use simplexml to parse xml files.

The SimpleXML function allows you to convert XML into an object. you can process this object by using a common attribute selector or an array iterator, just like processing any other object.

Some of these functions require the latest PHP version.

Installation:SimpleXML functions are a core component of PHP and can be used without installation. the code is as follows:

 Title; // because there are two author elements, the content of each author element is obtained cyclically $ authors = ''; foreach ($ sx-> author as $ author) {$ authors. = '&'. $ author;} $ authors = substr ($ authors, 1); echo $ title. ':'. $ authors; // The output result is as follows: // Php Book: kaifabu & Younker

PHP: indicates the earliest PHP version that supports this function.

SimpleXML functions

_ Construct () creates a new SimpleXMLElement object. 5

AddAttribute () adds an attribute to the SimpleXML element. 5

AddChild () adds a child element to the SimpleXML element. 5

AsXML () obtains an XML string from the SimpleXML element. 5

Attributes () gets the attributes of SimpleXML elements. 5

Children () gets the child of the specified node. 5

GetDocNamespaces () obtains the namespace of the XML document. 5

GetName () gets the name of the SimpleXML element. 5

GetNamespaces () obtains the namespace from XML data. 5

RegisterXPathNamespace () creates a namespace context for the next XPath query. 5

Simplexml_import_dom () gets the SimpleXMLElement object from the DOM node. 5

Simplexml_load_file () obtains the SimpleXMLElement object from the XML document. 5

Simplexml_load_string () obtains the SimpleXMLElement object from the XML string. 5

Article URL:

Reprint ^ at will, but please attach the tutorial address.

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.