PHP Operations XML Detailed (1/4)

Source: Internet
Author: User

1 Introduction to XML
XML is an abbreviation for the Extensible Identity Language (extensible Markup Language), a markup language similar to HTML. However, unlike HTML, XML is used primarily to describe data and store data, while HTML is used primarily to display data.
XML is a "meta tag" language in which developers can create tag names according to their needs. For example, the following XML code can be used to describe a message.

The code is as follows Copy Code

<thread>
<title>Welcome</title>
<author>Simon</author>
<content>welcome to XML guestbook!! </content>
</thread>

where,<thread> and </thread> tags are marked this is a message. In the message has the title, the author, the content, the complete expression a message information.
At the top of an XML file, you typically use <?xml version= "1.0″?>" to identify the start of XML data and the use of standard version information for XML data. accessing XML files in a browser can see hierarchical XML data information, as shown in Figure 1.


Photo: 1.jpg

XML development is very rapid, in recent years, many software developers have begun to use the XML development standards for application development. And, many emerging technologies are built on XML data. This means that XML will become an integral part of web technology as much as HTML.
2 Simple XML operations
In practical application, the interaction between PHP and XML is widely used. The SimpleXML component is a new addition to the PHP5 of a Jane
Single XML operation component, the use of SimpleXML components is very simple compared with traditional XML components. This section will use the
The SimpleXML component to manipulate the XML method is described in detail.
2.1 Creating a SimpleXML Object
SimpleXML objects are temporary variables used to temporarily store XML data, and operations on XML are done by manipulating simplexml objects. The SimpleXML component provides two ways to create a SimpleXML object. The first method is to use the Simplexml_load_string function to read the XML data in a string variable to complete the creation, and the syntax format is as follows.
Simplexml_load_string (String data)
Here the data variable is used to store XML data. The following code creates a SimpleXML object using the Simplexml_load_string function

The code is as follows Copy Code

$data = <<<xml//define XML data
<?xml version= ' 1.0′?>
<departs>
<depart>
<name>production support</name>
<employees>
<employee>
<serial_no>100001</serial_no>
<name>Simon</name>
<age>24</age>
<birthday>1982-11-06</birthday>
<salary>5000.00</salary>
<bonus>1000.00</bonus>
</employee>
<employee>
<serial_no>100002</serial_no>
<name>Elaine</name>
<age>24</age>
<birthday>1982-01-01</birthday>
<salary>6000.00</salary>
<bonus>2000.00</bonus>
</employee>
</employees>
</depart>
<depart>
<name>testing center</name>
<employees>
<employee>
<serial_no>110001</serial_no>
<name>Helen</name>
<age>23</age>
<birthday>1983-07-21</birthday>
<salary>5000.00</salary>
<bonus>1000.00</bonus>
</employee>
</employees>
</depart>
</departs>
XML;
$xml = simplexml_load_string ($data); Creating SimpleXML Objects
Print_r ($xml); Output XML
?>

In the example above, $data variable stores a piece of XML data. The simplexml_load_string function converts the variable $data into a SimpleXML object. The structure of the object can be seen through the output of the Print_r function, and the results of the operation are shown below.

The code is as follows Copy Code

SimpleXMLElement Object
(
[Depart] => Array
(
[0] => simplexmlelement Object
(
[Name] => production support
[Employees] => simplexmlelement Object
([Employee] => Array (
[0] => simplexmlelement Object
([Serial_no] => 100001
[Name] => Simon
[Age] => 24
[Birthday] => 1982-11-06
[Salary] => 5000.00
[Bonus] => 1000.00
)
[1] => simplexmlelement Object
([Serial_no] => 100002
[Name] => Elaine
[Age] => 24
[Birthday] => 1982-01-01
[Salary] => 6000.00
[Bonus] => 2000.00
)
)
)
)
[1] => simplexmlelement Object
(
[Name] => Testing Center
[Employees] => simplexmlelement Object
(
[Employee] => simplexmlelement Object
(
[Serial_no] => 110001
[Name] => Helen
[Age] => 23
[Birthday] => 1983-07-21
[Salary] => 5000.00
[Bonus] => 1000.00
)
)
)
)
)

As you can see from the output, the structure of the SimpleXML object is exactly the same as the format of the XML data.
The second method is to use the Simplexml_load_flie function to read an XML file to complete the creation, and the syntax format is as follows.
Simplexml_load_file (string filename)
The filename variable here is the filename and the path where the XML data file is stored. The following code uses the Simplexml_load_file function to create a SimpleXML object.

The code is as follows Copy Code

<?php
$xml = simplexml_load_file (' Example.xml '); Creating SimpleXML Objects
Print_r ($xml); Output XML
?>

Where the Example.xml store data is exactly the same as the $data above, and the results are exactly the same.
The above two methods implement the same function, the difference is that the data source of the XML is different. If the XML data source is in a php script file, you need to use simplexml_load_string to create it. If the XML data source is in a separate XML file, you need to use simplexml_load_file to create it.
2.2 Reading XML data in the SimpleXML object
The Print_r function is used to read the data in the SimpleXML object, and its return result is similar to the structure of the array. Obviously, this kind of display method is not advisable in practical application. Here are a few other ways to read the XML data in the SimpleXML object.

Home 1 2 3 4 last page

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.