This article mainly introduces SimpleXML function usage in PHP, analyzes in detail the skills for using SimpleXML function to operate XML files in the form of examples, and attaches the relevant SimpleXML function, for more information about SimpleXML functions in php, see the examples in this article. This function allows you to convert XML into objects and share it with you for your reference. The specific analysis is as follows:
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 part of the PHP core and can be used without installation. the code is as follows:
The code is as follows:
<? 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 .'
';
}
// Combination of simplexml and xpath is omnipotent
$ Words = $ lib-> xpath ("// word ");
// Method for obtaining attributes
Echo $ words [0] ['add'].'
';
Echo $ items [0] ['pp '];
?>
Related functions are described as follows:
Function |
Description |
PHP version |
_ Construct () |
Create a new SimpleXMLElement object |
5 |
AddAttribute () |
Add an attribute to the SimpleXML element |
5 |
AddChild () |
Add a child element to the SimpleXML element |
5 |
AsXML () |
Obtain an XML string from the SimpleXML element |
5 |
Attributes () |
Get SimpleXML element attributes |
5 |
Children () |
Obtains the subnode of a specified node. |
5 |
GetDocNamespaces () |
Get the namespace of the XML document |
5 |
GetName () |
Get SimpleXML element name |
5 |
GetNamespaces () |
Get namespace from XML data |
5 |
RegisterXPathNamespace () |
Create a namespace context for the next XPath query |
5 |
Simplexml_import_dom () |
Get SimpleXMLElement object from DOM node |
5 |
Simplexml_load_file () |
Get SimpleXMLElement object from XML document |
5 |
Simplexml_load_string () |
Get SimpleXMLElement object from XML string |
5 |
Xpath () |
Run XPath query on XML data |
5 |
I hope this article will help you with PHP programming.