This article briefly introduces how to use SimpleXML functions in php. This function allows you to convert XML into objects. For more information, see.
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 |
// 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
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