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:
$ 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 ."
";
}
}
$ M = $ xml-> getElementsByTagName ('M ');
Foreach ($ m as $ m)
{
$ List = $ m-> getElementsByTagName ("list ");
Foreach ($ list as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value ."
";
}
}
?>
Cy. php output result:
1
2
3
4
5
6
7
8
9
====================
When Php reads XML, cy. xml becomes more complex:
The XML file (cy. xml) is as follows:
The code is as follows:
A
1
2
3
B
4
5
6
C
7
8
9
So how can we read only Internal What is the value in?
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 ."
";
}
$ Title = $ main-> getElementsByTagName ("title ");
Foreach ($ title as $ title)
{
$ Value = $ title-> firstChild-> nodeValue;
Echo $ value ."
";
}
}
$ M = $ xml-> getElementsByTagName ('M ');
Foreach ($ m as $ m)
{
$ List = $ m-> getElementsByTagName ("list ");
Foreach ($ list as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value ."
";
}
}
?>
Cy. php output result:
1
2
3
A
4
5
6
7
8
9
Think about how to read Medium ?
The following is an example of how Php reads XML:
The XML file (cy. xml) is as follows:
The code is as follows:
This is Text One
This is Text Two
This is Text Three
This is Text Four
This is Text Five
This is Text Six
The php file (cy. php) is as follows:
The code is as follows:
$ Xml = new DOMDocument (); // Create a DOMDocument
$ Xml-> load ('cy. XML'); // Php specifies the location where the xml file needs to be read
$ LevelOne = $ xml-> getElementsByTagName ('levelone'); // Get the node by name and return the set of all nodes. However, reading LevelOne is meaningless here ....
$ LevelOne = $ xml-> getElementsByTagName ('levelone')-> item (0); // return the content of the first LevelOne node
$ LevelTwo = $ LevelOne-> getElementsByTagName ('leveltwo'); // Get the node by name and return all LevelTwo
Foreach ($ LevelTwo as $ Content) // read all LevelTwo cyclically and use Content to represent LevelTwo in the loop.
{
$ LevelThree = $ Content-> getElementsByTagName ('levelthree '); // return all LevelThree
Foreach ($ LevelThree as $ Concert)
{
$ Name = $ Concert-> nodeName; // Node name
$ Value = $ Concert-> nodeValue; // node value
$ Id = $ Concert-> getAttribute ('id'); // "id" attribute value
Echo $ name ."
";
Echo $ value ."
";
Echo $ id ."
";
}
}
?>
If you use $ LevelOne = $ xml-> getElementsByTagName ('levelone') to obtain a node, you need to use foreach loop to read the content, because $ LevelOne = $ xml-> getElementsByTagName ('levelone') returns a set, not a specific node-only one node called LevelOne ....
If you use $ LevelOne = $ xml-> getElementsByTagName ('levelone')-> item (0) to retrieve a node, read the content in it, you can directly $ LevelOne-> xxxxxx, because a specific node is returned.
Now we provide a simple way to read XML using PHP:
The XML file (cy. xml) is as follows:
The code is as follows:
Czbin xml section
Xml-related articles
Czbin php Forum
Php related articles
Czbin ajax Forum
Ajax-related articles
The php file (cy. php) is as follows:
The code is as follows:
$ Xml = simplexml_load_file ('sxml. XML ');
$ Part = $ xml-> site-> part;
Foreach ($ part as $ content)
{
Echo $ content ['id']."
";
Echo $ content-> title ."
";
Echo $ content-> title ['id']."
";
Echo $ content-> describe ."
";
}
?>
Cy. php output result:
1
Czbin xml section
A
Xml-related articles
2
Czbin php Forum
Php related articles
3
Czbin ajax Forum
C
Ajax-related articles
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.