PHP reads the XML value of the code of recommendation

  • 2020-03-31 21:21:57
  • OfStack

The simplest case of reading XML in Php:

The XML file (cy-xml) 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:
 
<?php 
$xml = new DOMDocument(); 
$xml->load('cy.xml'); 
foreach($xml->getElementsByTagName('list') as $list) 
{ 
$value = $list->firstChild->nodeValue; 
echo $value. " <br /> " ; 
} 
?> 

Run result of cy-php:
1
2
3

= = = = = = = = = = = = =

Reading XML in Php is a little more complicated:

Cy. XML 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 (no change from the first case):
The same code at the page code block index 1
The result of running cy-php is the same as the first time:
1
2
3

Php reads XML in the third case:

The XML file (cy-xml) 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 (same as the first time):
The same code at the page code block index 1
Output result of cy-php:
1
2
3
4
5
6

= = = = = = = = = = =

In the fourth case, keep cy-xml unchanged and change cy-php:

The XML file (cy-xml) is as follows:
The same code at the page code block index 4
The Php file (cy-php) 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 do the two versions of cy-php differ, but the output is the same? Let's look at the next example

= = = = = = = = = = = = = =

In the fifth case when Php reads XML, change cy-xml to keep cy-php in the fourth case:

The XML file (cy-xml) 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 file (cy-php) is the same as the fourth case:
The same code at the page code block index 7
The output result of cy-php is:
1
2
3
4
5
6

why
< M>
< List> 7 < / list>
< List> 8 < / list>
< List> 9 < / list>
< / m>
Is 7,8,9 not read?
Because our cy-php only reads < Main> < / main> Content in the tag. M> < / m> The contents of the tag will not be read.
The "tags" we refer to here are called "nodes" in XML;
We will explain the concept of "node" later.

Php reads XML case 6, foreach again, we read out 7,8,9! :

The XML file (cy-xm) 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:
 
<?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

= = = = = = = = = = = = = = =

Php reading XML case 7, cy-xml gets a little more complicated:

The XML file (cy-xml) 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 do we just read < Main> < / main> In < Title> < / title> What are the values of?

The Php file (cy-php) 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 it < M> In < Title> < / title> The value of?

Php reads the XML to reinforce it with an example:

The XML file (cy-xml) 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:
 
<?php 
$xml = new DOMDocument(); //Create a DOMDocument
$xml->load('cy.xml'); //Php specifies where the XML file needs to be read
$LevelOne = $xml->getElementsByTagName('LevelOne');//Get the node by name and return the set of all nodes, but there's no point reading LevelOne here... .
$LevelOne = $xml->getElementsByTagName('LevelOne')->item(0);//Returns the contents of the first LevelOne node
$LevelTwo = $LevelOne->getElementsByTagName('LevelTwo'); //Gets the node by name and returns all levels two
foreach ( $LevelTwo as $Content )//Read all levels two in a loop, and inside the loop, refer to LevelTwo as Content
{ 
$LevelThree = $Content->getElementsByTagName('LevelThree');//Return to all levels three
foreach ( $LevelThree as $Concert ) 
{ 
$name = $Concert->nodeName;//The name of the node
$value = $Concert->nodeValue;//Node values
$id = $Concert->getAttribute('id');//"Id" attribute values
echo $name. " <br /> " ; 
echo $value. " <br /> " ; 
echo $id. " <br /> " ; 
} 
} 
?> 

If you use $LevelOne = $XML -> GetElementsByTagName ('LevelOne') to get the node, then, to read the contents inside, you need a foreach loop, because $LevelOne = $XML -> GetElementsByTagName ('LevelOne') returns a collection, not a specific node - there is only one node called LevelOne... .
If you use $LevelOne = $XML -> GetElementsByTagName (' LevelOne) - > If item(0) is the way to get the node, then the contents inside can be read, you can directly $LevelOne-> XXXXXX, because this returns a concrete node.

Here's an easy way to read XML in PHP:

The XML file (cy-xml) 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 section </title> 
<describe>php Related articles </describe> 
</part> 
<part id= " 3 " > 
<title id= " c " >czbin ajax section </title> 
<describe>ajax Related articles </describe> 
</part> 
</site> 
</xml> 

The Php file (cy-php) 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.
Articles on XML
2
Czbin PHP section

PHP articles
3
Czbin ajax edition piece
c
Ajax articles

How's that ? Simple indeed!

Related articles: