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.

two, the way PHP reads XML

XML source files

<?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

<?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-$old \ n";
}
?>

2) simplexml read XML

<?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 PHP Regular expression 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 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

<?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 of the above four 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 label, the equivalent of jquery in the text () method, And XmlReader? He's 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,

<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 (");

The above is purely personal opinion, please correct me. Hope to be helpful to everyone.



Related Article

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.