<? Xml version = "1.0 encoding =" UTF-8 "?>
<Humans>
<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 reads xml
<? Php
$ Doc = new DOMDocument ();
$ Doc-> load ('person. xml'); // read the xml file
$ Humans = $ doc-> getElementsByTagName_r ("humans"); // get the object array of the humans tag
Foreach ($ humans as $ human)
{
$ Names = $ human-> getElementsByTagName_r ("name"); // an array of objects that obtain the name tag
$ Name = $ names-> item (0)-> nodeValue; // obtain the value in the node, as shown in <name> </name>
$ Sexs = $ human-> getElementsByTagName_r ("sex ");
$ Sex = $ sexs-> item (0)-> nodeValue;
$ Olds = $ human-> getElementsByTagName_r ("old ");
$ Old = $ olds-> item (0)-> nodeValue;
Echo "$ name-$ sex-$ old \ n ";
}
?>
2) simplexml reading xml
<? Php
$ Xml_array = simplexml_load_file ('person. xml'); // read the data in xml to the array object.
Foreach ($ xml_array as $ tmp ){
Echo $ tmp-> name. "-". $ tmp-> sex. "-". $ tmp-> old. "<br> ";
}
?>
3) Use php regular expression to remember data/http://www.pprar.com
<? 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 the name
Preg_match_all ("/\ <sex \> (.*?) \ <\/Sex \>/", $ human, $ sex); // match 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 reads xml data
<? Php
$ Reader = new XMLReader ();
$ Reader-> open ('person. xml'); // read xml data
$ I = 1;
While ($ reader-> read () {// whether to read
If ($ reader-> nodeType = XMLReader: TEXT) {// judge the node type
If ($ I % 3 ){
Echo $ reader-> value; // get the node value
} Else {
Echo $ reader-> value. "<br> ";
}
$ I ++;
}
}
?>
Iii. Summary
There are many ways to read xml, just a few simple examples. The above four methods can read the data in the tag, <name> </name>. however, their test focus is different. The design focus of the function reading xml in the first three methods is to read the values in the tag, which is equivalent to the text () method in jquery, xmlreader is not the same as xmlreader. Instead, xmlreader focuses on reading the values in tags and reading the attributes of tags to transmit data, all are placed in the attribute (but the method I wrote above still takes the value in the tag, because the xml file has been given, and I don't want to get the xml file out ). For example,
<Data name = 'zhang ying' sex = 'male' old = '28'> </data>
Xmlreader is designed to read the value of name sex old in data, and reading the content of <data> </data> is troublesome. It is equivalent to attr (") in jquery.
If you have a good way to read xml, you can add