Introduction to PHP Read XML method
One, what is the purpose of Xml,xml XML (extensible Markup Language) Extensible Markup Language, which, like HTML, is SGML (Standard generalized Markup Language, Standard Universal Markup Language). XML is a cross-platform in the Internet environment ...
One, what is the purpose of xml,xml?
XML (extensible Markup Language) extends the markup language, which, like HTML, is SGML (Standard generalized Markup Language, the standardized universal Markup language). XML is a cross-platform, content-dependent technology in an Internet environment, and is a powerful tool for the current processing of structured document information. 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 occupies more space than binary data, but XML is extremely simple and easy to master and use.
XML has many uses, can be used to store data, can be used to do data exchange, for many kinds of application software to prompt data and so on.
Second, the way PHP reads XML
XML source file
<?xml version= "1.0 encoding=" UTF-8 "?>
<zhangying>
<name> Zhang Ying </name>
<sex> Men </sex>
<old>28</old>
</zhangying>
<tank>
<name>tank</name>
<sex> Men </sex>
<old>28</old>
</tank>
1) DOMDocument Read XML
<?php
$doc = new DOMDocument ();
$doc->load (' person.xml '); Reading an XML file
$humans = $doc->getelementsbytagname ("humans"); An array of objects that get humans tags
foreach ($humans as $human)
{
$names = $human->getelementsbytagname ("name"); An array of objects that get the label of name
$name = $names->item (0)->nodevalue; Get 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
<?php
$xml _array=simplexml_load_file (' person.xml '); Reads the data from the XML into the array object
foreach ($xml _array as $tmp) {
echo $tmp->name. " -". $tmp->sex." -". $tmp->old." <br> ";
}
?>
3) Use PHP Regular expressions to memorize the data
<?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 out gender
Preg_match_all ("/\<old\> (. *?) \<\/old\>/", $human, $old); Match up Age
}
foreach ($name [1] as $key = + $val) {
echo $val. "-" $sex [$key][1]. "-". $old [$key][1]. " <br> ";
}
?>
4) XmlReader to read XML data
<?php
$reader = new XMLReader ();
$reader->open (' person.xml '); Reading XML data
$i = 1;
while ($reader->read ()) {//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 + +;
}
}
?>
Three, summary
There are many ways to read XML, just to name a few. The above four methods can read the data in the label, Zhang Ying. But their focus is different, the first three methods of reading the function of XML is designed to read the value in the tag, equivalent to the text () method in jquery, and XmlReader he is not quite the same, His focus is not to read the value in the tag, but to read the label's properties and put the data to be transferred in the attribute (although 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 the XML file out).
As an example,
<data name= ' Zhang Ying ' sex= ' male ' old= ' 28′></data>
The XmlReader is designed to read the value of the name sex old in data, and the content is more cumbersome to read. He is the equivalent of jquery in attr (");
The above is purely personal opinion, please correct me. Hope to be of help to everyone.
Introduction to PHP Read XML method