PHP code for reading XML values (recommended)

Source: Internet
Author: User
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:

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.