Apply DOM to hold XML in PHP5

Source: Internet
Author: User
The XML support is enhanced in PHP5, and the application of DOM expands the XML control capability. These functions are part of the PHP5 core and can be applied without installation. The following example briefly demonstrates DOM's control over XML. For more information, see the comments in the code? /

The XML support is enhanced in PHP5, and the application of DOM expands the XML control capability. These functions are part of the PHP5 core and can be applied without installation.

The following example briefly demonstrates DOM's control over XML. For more information, see the comments in the code.

/*************************************** *********
** Use XML in PHP5
** Reference site:
** Http://cn.php.net/manual/zh/ref.dom.php
** The follow codes need PHP5 support
**************************************** *********/


// Create a DOMDocument object first
$ Dom = new DomDocument ();
// Then load the file into the XML file
$ Dom-> load ('test. XML ');

// Output the XML file
// Header ('content-type: text/xml; charset = gb2312 ');
// Echo $ dom-> saveXML ();

// Keep the XML file. the returned value is int (file size, in bytes)
// $ Dom-> save ('newfile. XML ');

Echo'

Obtain all the title elements: ';
$ Titles = $ dom-> getElementsByTagName ('title ');
Foreach ($ titles as $ node)
{
Echo $ node-> textContent .'
';
// You can also
// Echo $ node-> firstChild-> data .'
';
}

/*
Echo'

Traverse all nodes from the root node:
';
Foreach ($ dom-> documentElement-> childNodes as $ items ){
// If the node is an element (nodeType = 1) and its name is "item", The Loop continues.
If ($ items-> nodeType = 1 & $ items-> nodeName = 'ITEM '){
Foreach ($ items-> childNodes as $ titles ){
// If a node is an element and its name is title, print it.
If ($ titles-> nodeType = 1 & $ titles-> nodeName = 'TITLE '){
Print $ titles-> textContent. '\ n ';
}
}
}
}
*/

// Use XPath to query data
Echo'

Result of the title node queried using XPath: ';
$ Xpath = new domxpath ($ dom );
$ Titles = $ xpath-> query ('/rss/channel/item/title ');
Foreach ($ titles as $ node)
{
Echo $ node-> textContent .'
';
}
/*
This is slightly different from the getElementsByTagName () method, but Xpath is much more powerful.
A deep dive may be like this:
/Rss/channel/item [position () = 1]/title returns all
/Rss/channel/item/title [@ id = '23'] return all the titles that contain the id attribute and whose value is 23.
/Rss/channel/& folder &/title returns the title under all articles elements)
*/


// Write new data to the DOM
$ Item = $ dom-> createElement ('ITEM ');
$ Title = $ dom-> createElement ('title ');
$ TitleText = $ dom-> createTextNode ('title text ');
$ Title-> appendChild ($ titleText );
$ Item-> appendChild ($ title );
$ Dom-> documentElement-> getElementsByTagName ('channel')-> item (0)-> appendChild ($ item );

Related Article

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.