The simplest case of reading XML with Php:
The XML file (cy. xml) is as follows:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<List> 1 </list>
<List> 2 </list>
<List> 3 </list>
</Xml>
The php file (cy. php) is as follows:
Copy codeThe Code is as follows:
<? Php
$ Xml = new DOMDocument ();
$ Xml-> load ('cy. xml ');
Foreach ($ xml-> getElementsByTagName ('LIST') as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value. "<br/> ";
}
?>
Cy. php running result:
1
2
3
==================
Reading XML with Php is a little complicated:
Cy. xml is as follows:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<Main>
<List> 1 </list>
<List> 2 </list>
<List> 3 </list>
</Main>
</Xml>
Cy. php is as follows (and there is no change in the first case ):
Copy codeThe Code is as follows:
<? Php
$ Xml = new DOMDocument ();
$ Xml-> load ('cy. xml ');
Foreach ($ xml-> getElementsByTagName ('LIST') as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value. "<br/> ";
}
?>
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:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<Main>
<List> 1 </list>
<List> 2 </list>
<List> 3 </list>
</Main>
<Main>
<List> 4 </list>
<List> 5 </list>
<List> 6 </list>
</Main>
</Xml>
Php file (cy. php) is as follows (and remains unchanged for the first time ):
Copy codeThe Code is as follows:
<? Php
$ Xml = new DOMDocument ();
$ Xml-> load ('cy. xml ');
Foreach ($ xml-> getElementsByTagName ('LIST') as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value. "<br/> ";
}
?>
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:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<Main>
<List> 1 </list>
<List> 2 </list>
<List> 3 </list>
</Main>
<Main>
<List> 4 </list>
<List> 5 </list>
<List> 6 </list>
</Main>
</Xml>
The php file (cy. php) is as follows:
Copy codeThe Code is as follows:
<? Php
$ 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. "<br/> ";
}
}
?>
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:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<Main>
<List> 1 </list>
<List> 2 </list>
<List> 3 </list>
</Main>
<Main>
<List> 4 </list>
<List> 5 </list>
<List> 6 </list>
</Main>
<M>
<List> 7 </list>
<List> 8 </list>
<List> 9 </list>
</M>
</Xml>
Php files (cy. php) are the same as those in the fourth scenario:
Copy codeThe Code is as follows:
<? Php
$ 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. "<br/> ";
}
}
?>
Cy. php output:
1
2
3
4
5
6
Why?
<M>
<List> 7 </list>
<List> 8 </list>
<List> 9 </list>
</M>
Which of the following are not read?
Because our cy. php only reads the content in the <main> </main> MARK. <m> </m> mark, the content is not 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:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<Main>
<List> 1 </list>
<List> 2 </list>
<List> 3 </list>
</Main>
<Main>
<List> 4 </list>
<List> 5 </list>
<List> 6 </list>
</Main>
<M>
<List> 7 </list>
<List> 8 </list>
<List> 9 </list>
</M>
</Xml>
The php file (cy. php) is as follows:
Copy codeThe Code is as follows:
<? Php
$ 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. "<br/> ";
}
}
$ M = $ xml-> getElementsByTagName ('M ');
Foreach ($ m as $ m)
{
$ List = $ m-> getElementsByTagName ("list ");
Foreach ($ list as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value. "<br/> ";
}
}
?>
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:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<Main>
<Title> a </title>
<List> 1 </list>
<List> 2 </list>
<List> 3 </list>
</Main>
<Main>
<Title> B </title>
<List> 4 </list>
<List> 5 </list>
<List> 6 </list>
</Main>
<M>
<Title> c </title>
<List> 7 </list>
<List> 8 </list>
<List> 9 </list>
</M>
</Xml>
So how can we read only the values in <main> </main> <title> </title>?
The php file (cy. php) is as follows:
Copy codeThe Code is as follows:
<? Php
$ 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. "<br/> ";
}
$ Title = $ main-> getElementsByTagName ("title ");
Foreach ($ title as $ title)
{
$ Value = $ title-> firstChild-> nodeValue;
Echo $ value. "<br/> ";
}
}
$ M = $ xml-> getElementsByTagName ('M ');
Foreach ($ m as $ m)
{
$ List = $ m-> getElementsByTagName ("list ");
Foreach ($ list as $ list)
{
$ Value = $ list-> firstChild-> nodeValue;
Echo $ value. "<br/> ";
}
}
?>
Cy. php output result:
1
2
3
A
4
5
6
7
8
9
Think about how to read the value of <m> <title> </title> in <m>?
The following is an example of how Php reads XML:
The XML file (cy. xml) is as follows:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<LevelOne>
<LevelTwo>
<LevelThree id = "1"> This is Text One </LevelThree>
<LevelThree id = "2"> This is Text Two </LevelThree>
<LevelThree id = "3"> This is Text Three </LevelThree>
</LevelTwo>
<LevelTwo>
<LevelThree id = "4"> This is Text Four </LevelThree>
<LevelThree id = "5"> This is Text Five </LevelThree>
<LevelThree id = "6"> This is Text Six </LevelThree>
</LevelTwo>
</LevelOne>
The php file (cy. php) is as follows:
Copy codeThe Code is as follows:
<? Php
$ 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. "<br/> ";
Echo $ value. "<br/> ";
Echo $ id. "<br/> ";
}
}
?>
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:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "gb2312"?>
<Xml>
<Site>
<Part id = "1">
<Title id = "a"> czbin xml section </title>
<Describe> xml-related articles </describe>
</Part>
<Part id = "2">
<Title id = "B"> czbin php Forum </title>
<Describe> php related articles </describe>
</Part>
<Part id = "3">
<Title id = "c"> czbin ajax Forum </title>
<Describe> ajax-related articles </describe>
</Part>
</Site>
</Xml>
The php file (cy. php) is as follows:
Copy codeThe Code is as follows:
<? Php
$ Xml = simplexml_load_file ('sxml. xml ');
$ Part = $ xml-> site-> part;
Foreach ($ part as $ content)
{
Echo $ content ['id']. "<br/> ";
Echo $ content-> title. "<br/> ";
Echo $ content-> title ['id']. "<br/> ";
Echo $ content-> describe. "<br/> ";
}
?>
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
How is it? It's really easy!