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.
Two, PHP method of reading XML XML source file
Copy CodeThe code is as follows:
Zhang Ying
Man
28
Tank
Man
28
1) DOMDocument Read XML
Copy CodeThe code is as follows:
$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; Gets the value in node, such as
$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
Copy CodeThe code is as follows:
$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."
";
}
?>
3) Use PHP Regular expressions to memorize the data
Copy CodeThe code is as follows:
$xml = "";
$f = fopen (' person.xml ', ' R ');
while ($data = Fread ($f, 4096)) {
$xml. = $data;
}
Fclose ($f);
Read Data above
Preg_match_all ("/\ (.*?) \<\/humans\>/s ", $xml, $humans); Match the contents of the outermost label
foreach ($humans [1] as $k = $human)
{
Preg_match_all ("/\ (.*?) \<\/name\>/", $human, $name); Match a name
Preg_match_all ("/\ (.*?) \<\/sex\>/", $human, $sex); Match out gender
Preg_match_all ("/\ (. *?) \<\/old\>/", $human, $old); Match age
}
foreach ($name [1] as $key = + $val) {
echo $val. "-". $sex [$key][1]. "-" $old [$key][1]. "
";
}
?>
4) XmlReader to read the XML data
copy code code as follows:
!--? php $reader = new XMLReader ();
$reader->open (' person.xml ');//Read 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.
";
}
$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).
For an example,
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.