How does PHP read XML files? PHP reads an XML document example

Source: Internet
Author: User
Tags foreach json 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 Copy Code

<?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>
</ Humans>
1) domdocument read XML
<?php
$doc = new DOMDocument ();
$doc->load (' person.xml ');//Read XML file
$humans = $doc->getelementsbytagname ("humans");//Get Humans Tag object array
foreach ($humans as $human)
{
$names = $human->getelementsbytagname ("name");//Get an array of objects for the label of name
$n Ame = $names->item (0)->nodevalue; Gets the value 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-$old \ n";
}

2) simplexml Read XML

The code is as follows Copy Code
<?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> ";
}
?>
If you use curl to get the XML data
$xml = simplexml_load_string ($data);
$data [' tk '] = Json_decode (Json_encode ($xml), TRUE);
If you're getting URL data directly,
$xml = Simplexml_load_file ($data);
$data [' tk '] = Json_decode (Json_encode ($xml), TRUE);

Convert the SimpleXML object to JSON, and then convert the JSON to an array.
3 Use PHP Regular expression to memorize the data

The code is as follows Copy Code
<?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); Match a name
Preg_match_all ("/\<sex\>" (. *?) \<\/sex\>/", $human, $sex); Match a gender
Preg_match_all ("/\<old\>" (. *?) \<\/old\>/", $human, $old); Match out of age
}
foreach ($name [1] as $key => $val) {
echo $val. "-". $sex [$key][1]. "-" $old [$key][1]. " <br> ";
}
?>

4) XmlReader to read XML data

The code is as follows Copy Code
<?php
$reader = new XMLReader ();
$reader->open (' person.xml '); Reading XML data
$i = 1;
while ($reader->read ()) {//is read
if ($reader->nodetype = = Xmlreader::text) {//Determine node type
if ($i%3) {
Echo $reader->value; Gets the value of node
}else{
echo $reader->value. " <br> ";
}
$i + +;
}
}
?>

Third, summary

There are a lot of ways to read XML, simply to mention 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).
To illustrate, for example,

The code is as follows Copy Code

<data name= ' Zhang Ying ' sex= ' old= ' 28′></data>

XmlReader's design focus is to read the value of the name sex old in data, and the content is more cumbersome to read. 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.