PHP reads the XML value code. Many of the Code on the Internet is in pdf version. The conversion earn points. The script is specially designed for searching and sorting. I hope you will support us a lot in the future.
PHP reads the XML value code. Many of the Code on the Internet is in pdf version. The conversion earn points. The script is specially designed for searching and sorting. I hope you will support us a lot in the future.
The simplest case of reading XML with Php:
The XML file (cy. xml) is as follows:
The Code is as follows:
1
2
3
The php file (cy. php) is as follows:
The Code is as follows:
$ Xml = new DOMDocument ();
$ Xml-> load ('cy. xml ');
Foreach ($ xml-> getElementsByTagName ('LIST') as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value ."
";
}
?>
Cy. php running result:
1
2
3
==================
Reading XML with Php is a little complicated:
Cy. xml is as follows:
The Code is as follows:
1
2
3
Cy. php is as follows (and there is no change in the first case ):
The Code is as follows:
$ Xml = new DOMDocument ();
$ Xml-> load ('cy. xml ');
Foreach ($ xml-> getElementsByTagName ('LIST') as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value ."
";
}
?>
The running result of cy. php is the same as that of the first time:
1
2
3
Php reads XML in the third case:
The XML file (cy. xml) is as follows:
The Code is as follows:
1
2
3
4
5
6
Php file (cy. php) is as follows (and remains unchanged for the first time ):
The Code is as follows:
$ Xml = new DOMDocument ();
$ Xml-> load ('cy. xml ');
Foreach ($ xml-> getElementsByTagName ('LIST') as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value ."
";
}
?>
Cy. php output result:
1
2
3
4
5
6
==============
Php reads XML In the fourth case, keeping cy. xml unchanged and changing cy. php:
The XML file (cy. xml) is as follows:
The Code is as follows:
1
2
3
4
5
6
The php file (cy. php) is as follows:
The Code is as follows:
$ Xml = new DOMDocument ();
$ Xml-> load ('cy. xml ');
$ Main = $ xml-> getElementsByTagName ('main ');
Foreach ($ main as $ main)
{
$ List = $ main-> getElementsByTagName ("list ");
Foreach ($ list as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value ."
";
}
}
?>
Cy. php output result:
1
2
3
4
5
6
Why are the output results of cy. php different twice the same? Let's look at the next example.
====================
Php reads XML in the fifth case, and changes cy. xml to keep cy. php In the fourth case:
The XML file (cy. xml) is as follows:
The Code is as follows:
1
2
3
4
5
6
7
8
9
Php files (cy. php) are the same as those in the fourth scenario:
The Code is as follows:
$ Xml = new DOMDocument ();
$ Xml-> load ('cy. xml ');
$ Main = $ xml-> getElementsByTagName ('main ');
Foreach ($ main as $ main)
{
$ List = $ main-> getElementsByTagName ("list ");
Foreach ($ list as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value ."
";
}
}
?>
Cy. php output:
1
2
3
4
5
6
Why?
7
8
9
Which of the following are not read?
Because our cy. php only reads Content in the tag. The content in the tag will not be read.
The mark we mentioned here is called a "Node" in XML ";
The concept of "Node" will be described later.
Php reads XML 6 and foreach again. Let's read 7, 8, and 9! :
The XML file (cy. xm) is as follows:
The Code is as follows:
1
2
3
4
5
6
7
8
9
The php file (cy. php) is as follows:
The Code is as follows: