Introduction to PHP reading XML methods

Source: Internet
Author: User
Tags foreach php regular expression regular expression

One, what is Xml,xml's purpose?

XML (extensible Markup Language) expands the markup language, which, like HTML, is SGML (Standard generalized Markup Language, Standard Universal Markup Language). XML is a cross-platform, content-dependent technology in an Internet environment and a powerful tool for dealing with structured document information at the moment. Extensible Markup Language XML is a simple data storage language that uses a series of simple tags to describe data that can be built in a convenient way, although XML takes up more space than binary data, but XML is extremely simple and easy to master and use.
XML can be used to store data, to exchange data, to prompt data for many kinds of applications, and so on.
Second, the way PHP reads XML
XML source files
The code is as follows:
<?xml version= "1.0 encoding=" UTF-8 "?>"
<zhangying>
<name> Zhang Ying </name>
<sex> male </sex>
<old>28</old>
</zhangying>
<tank>
<name>tank</name>
<sex> male </sex>
<old>28</old>
</tank>


1) DOMDocument Read XML
The code is as follows:
<?php
$doc = new DOMDocument ();
$doc->load (' person.xml '); reading XML files
$humans = $doc->getelementsbytagname ("humans"); An array of objects to get the humans label
foreach ($humans as $human)
{
$names = $human->getelementsbytagname ("name"); An array of objects that gets the label for name
$name = $names->item (0)->nodevalue; Get the values in node, such as <name> </name>
$sexs = $human->getelementsbytagname ("sex");
$sex = $sexs->item (0)->nodevalue;
$olds = $human->getelementsbytagname ("old");
$old = $olds->item (0)->nodevalue;
echo "$name-$sex-$OLDN";
}
?>


2) simplexml Read XML
The code is as follows:
<?php
$xml _array=simplexml_load_file (' person.xml '); Reads data from XML into an array object
foreach ($xml _array as $tmp) {
echo $tmp->name. " -". $tmp->sex." -". $tmp->old." <br> ";
}
?>


3) Use the PHP regular expression to memorize the data
code as follows:
<?php
$xml = "";
$f = fopen (' person.xml ', ' R ');
while ($data = Fread ($f, 4096)) {
$xml. = $data;
}
Fclose ($f);
//Read data above
Preg_match_all ("/ foreach ($humans [1] as $k => $human)
{
Preg_match_all ("/<name>") (. *?) </name>/", $human, $name); Matches the name of the
Preg_match_all ("/<sex>" (. *?) </sex>/", $human, $sex); Matches the gender
Preg_match_all ("/<old>" (. *?) </old>/", $human, $old); Match the Age
}
foreach ($name [1] as $key => $val) {
echo $val. "-". $sex [$key][1]. "$old [$key][1]." <br> ";
}


4) XMLReader to read the XML data
code as follows:
<?php
$reader = new XMLReader ();
$reader->open (' person.xml '); /Read the XML data
$i =1
while ($reader->read ()) {//whether to read the
if ($reader->nodetype = = Xmlreader::text) {//Judge node type br> if ($i%3) {
echo $reader->value;//Get node value
}else{
Echo $reader->value. " <br> ";
}
$i + +;
}
}

Three, summary
There are many ways to read XML, simply to name a few. All four of the above methods are able to read the data in the tag, Zhang Ying. But their focus is different, the first three methods of reading the XML function is designed to read the value in the tag, the equivalent of jquery in the text () method, and XmlReader he is not the same, He's not focused on reading the values in the label, and read the properties of the tag, put the data to be transferred, all in the attribute (but the method I wrote above still takes the value in the tag, because the XML file is already given, I don't want to get out of the XML file).
For example,
<data name= ' Zhang Ying ' sex= ' old= ' 28′></data>
XmlReader is designed to read the value of name sex old in data. and read the content is more trouble. He is equivalent to the attr in jquery (");

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.