PHP SimpleXML

Source: Internet
Author: User

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.

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.