Introduction to SimpleXML functions in PHP

Source: Internet
Author: User
Tags xpath

The SimpleXML function allows you to convert XML into objects.

This object can be processed by using a common attribute selector or array iterator, just like any other object.

Some of these functions require the latest PHP version.
Install
SimpleXML functions are a core component of PHP. These functions can be used without installation.

 

The code is as follows: Copy code
<? Php
// Return all content as an object
$ Lib = simplexml_load_file ("cet4.xml ");
// Return the obtained item node in array form
$ Items = $ lib-> item;
$ WordsLength = sqlserver/42852.htm target = _ blank> count ($ items );
// Obtain the node values of all nodes
For ($ I = 0; $ I <$ wordsLength; $ I ++ ){
$ Word = $ items [$ I];
Echo $ word-> word. '-----'. $ word-> trans. '-----'. $ word-> phonetic. '<br/> ';
 }
// Combination of simplexml and xpath is omnipotent
$ Words = $ lib-> xpath ("// word ");
// Method for obtaining attributes
Echo $ words [0] ['add']. '<br/> ';
Echo $ items [0] ['pp '];
?>

Related functions

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

Function description PHP
_ 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
Xpath () runs XPath queries on XML data. 5

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.