PHP SimpleXML
SimpleXML processes the most common XML tasks and blades, with other extensions.
What is SimpleXML?
SimpleXML is the new PHP 5. This is a simple way to get the attributes and text of a element, if you know the layout of the XML file.
Compared to DOM or foreign analyzer, SimpleXML only requires a few lines of code to read text data from a component.
SimpleXML converts an XML document to an object, as shown in the following code:
Content-convert to a single attribute S impleXMLElement object. When there is more than one element at a level, they put an array
Attribute-use an associated array for access. If the index corresponds to the attribute name
Metadata-data is converted from text content to strings. If one element has more than one text node, they will be arranged in the command and they will find
SimpleXML is fast and easy to use. Basic tasks include:
Read XML files
Extract data from an XML string
Edit a text node or attribute
However, when advanced processing of XML, such as names, you are the best foreigner to use analyzer or xml dom.
Install
From PHP 5.0, SimpleXML is a core part of PHP. These functions can be used without installation.
Use SimpleXML
The following is an XML file:
<? Xml version = "1.0" encoding = "ISO-8859-1"?> <Note> <to> Tove </to> <from> Jani </from>
We want to output the element name and data above the XML file.
What is this:
Load XML files
The first element of the obtained name.
Creating a loop will trigger each child node and use the Child () function
Name and data of the output element.
For example
<? Php $ xml = simplexml_load_file ("test. xml ");
Echo $ xml-> getName (). "<br/> ";
Foreach ($ xml-> children () as $ child) {echo $ child-> getName (). ":". $ child. "<br/>" ;}?>
The output result is
Noteto: Tovefrom: Janiheading: Reminderbody: Don't forget me this weekend!
For more information, see www.111cn.net/phper/php.html.