The simplest case of reading XML with Php:
The XML file (cy.xml) is as follows:
Copy the code The code is as follows:
1
2
3
The Php file (cy.php) is as follows:
Copy code 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 execution result of cy.php:
1
2
3
==============
A slightly more complicated situation of reading XML with Php:
cy.xml is as follows:
Copy code The code is as follows:
>
1
2
3
cy.php is as follows (no changes from the first case):
Copy the code 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 result of running cy.php is the same as the first time:
1
2
3
The third case when Php reads XML:
The XML file (cy.xml) is as follows:
Copy code The code is as follows:
< ;list>1
2
3
4
5
6
The Php file (cy.php) is as follows (still unchanged from the first time):
Copy the code The code is as follows:
$xml = new DOMDocument();
$xml->load('cy.xml');
foreach($xml-> getElementsByTagName('list') as $list)
{
$value = $list->firstChild->nodeValue;
echo $value.”
”;
}
?>
Output result of cy.php:
1
2
3
4
5
6
============
The fourth case of XML reading by Php, keep cy.xml unchanged, change cy.php:
XML file ( cy.xml) as follows:
Copy code The code is as follows:
1
2
3
4
5
The Php file (cy.php) is as follows:
Copy code 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 results:
1
2
3
4
5
6
Why are the two cy.php different, but the output The result is the same? Let's look at the next example
==============
Php reads XML fifth case, change cy. xml, keep cy.php in the fourth situation:
XML file (cy.xml) is as follows:
Copy code The code is as follows:
1
2
3
5
6
< list>7
8
9
The Php file (cy.php) is the same as the fourth case:
Copy the code 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 is:
1
2
3
4
5
6
Why
7
8
9
The 7,8,9 in are not read out?
Because our cy.php only reads the content in the
tag.
tag The content of will not be read.
The "mark" we mentioned here is called "node" in XML;
We will explain the related concepts of "node" later.
Php reads XML situation 6, foreach again, we read out 7, 8, 9!:
The XML file (cy.xm) is as follows:
Copy code The code is as follows:
1
2
3
4
5
6
7
8
9
m>
The Php file (cy.php) is as follows:
Copy code 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 results:
1
2
3
4
5
6
7
8
9
===============
Php reads XML situation 7, cy.xml It becomes more complicated:
The XML file (cy.xml) is as follows:
Copy the code The code is as follows:
a
1
2
3
b
4
5
6
c
7
8
9
那么,我们如何只读出
内
中的值呢?
Php文件(cy.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.”
”;
}
$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输出结果:
1
2
3
a
4
5
6
7
8
9
想一下,如何读出
中的值?
Php读取XML再通过一个例子巩固一下:
XML文件(cy.xml)如下:
复制代码 代码如下:
This is Text One
This is Text Two
This is Text Three
This is Text Four
This is Text Five
This is Text Six
Php文件(cy.php)如下:
复制代码 代码如下:
$xml = new DOMDocument(); //建立一个DOMDocument
$xml->load('cy.xml'); //Php指定需要读取xml文件的位置
$LevelOne = $xml->getElementsByTagName('LevelOne');//按照名称取得节点,返回所有节点的集合,不过这里这样读LevelOne是没有意义的….
$LevelOne = $xml->getElementsByTagName('LevelOne')->item(0);//返回第一个LevelOne节点中的内容
$LevelTwo = $LevelOne->getElementsByTagName('LevelTwo'); //按照名称取得节点,返回所有LevelTwo
foreach ( $LevelTwo as $Content )//循环读出所有LevelTwo,并在循环里,把LevelTwo用Content表示
{
$LevelThree = $Content->getElementsByTagName('LevelThree');//返回所有LevelThree
foreach ( $LevelThree as $Concert )
{
$name = $Concert->nodeName;//节点名称
$value = $Concert->nodeValue;//节点值
$id = $Concert->getAttribute('id');//”id”属性值
echo $name.”
”;
echo $value.”
”;
echo $id.”
”;
}
}
?>
If you use $LevelOne = $xml->getElementsByTagName('LevelOne') to obtain the node, then you need to use a foreach loop to read the content inside, because $LevelOne = $xml->getElementsByTagName (' LevelOne') returns a collection, not a specific node - even though there is only one node called LevelOne...
If you use $LevelOne = $xml->getElementsByTagName('LevelOne')-> If you obtain the node through item(0), then you can read the content directly by $LevelOne->xxxxxx, because this returns a specific node.
Now provides a very simple method to read XML with PHP:
The XML file (cy.xml) is as follows:
Copy code The code is as follows:
czbin xml section
xml related articles
czbin php section
php related Article
czbin ajax section
ajax related articles
Php file ( cy.php) as follows:
Copy code 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 section
php related articles
3
czbin ajax How about
c
ajax related articles
? It's really simple!
http://www.bkjia.com/PHPjc/322729.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322729.htmlTechArticleThe simplest case of reading XML with Php: The XML file (cy.xml) is as follows: Copy the code as follows: ?xml version=”1.0″ encoding=”gb2312″? xml list1/list list2/list list3/list /xml Php file...