第一个例子:
PHP与XML联系紧密,下面是一个较简单的解析XML节点数据的例子,示例中自定义有一个XML样例文件代码,通过PHP将依次读取出XML节点中的id、name和email数据,是读取较大的XML文件的前提,新手的话,那就更应该参考了:
01 <?php 02 //定义一个XML样例文件 03 $xml_string="<?xml version=‘1.0‘?> 04 <users> 05 <user id=‘398‘> 06 <name>Foo</name> 07 <email>[email protected]</name> 08 </user> 09 <user id=‘867‘> 10 <name>Foobar</name> 11 <email>[email protected]</name> 12 </user> 13 </users>"; 14 //开始解析XML 15 $xml = simplexml_load_string($xml_string); 16 foreach ($xml->user as $user) 17 { 18 echo $user[‘id‘], ‘ ‘; 19 echo $user->name, ‘ ‘; 20 echo $user->email, ‘<br />‘; 21 } 22 ?>
第二个例子:
Php读取XML情况六,再foreach一次,我们把7,8,9读出来!:
XML文件(cy.xm)如下:
<?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文件(cy.php)如下:
<?php
$xml = new DOMDocument();
$xml->load(‘cy.xml‘);
$main = $xml->getElementsByTagName_r(‘main‘);
foreach( $main as $main)
{
$list = $main->getElementsByTagName_r( "list" );
foreach ( $list as $list )
{
$value =
$list->firstChild->nodeValue;
echo $value."<br />";
}
}
$m = $xml->getElementsByTagName_r(‘m‘);
foreach( $m as $m)
{
$list = $m->getElementsByTagName_r( "list" );
foreach ( $list as $list )
{
$value =
$list->firstChild->nodeValue;
echo $value."<br />";
}
}
?>
cy.php输出结果:
1
2
3
4
5
6
7
8
9
===============
Php读取XML情况七,cy.xml变得更复杂一些:
XML文件(cy.xml)如下:
<?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>
那么,我们如何只读出<main></main>内<title></title>中的值呢?
Php文件(cy.php)如下:
<?php
$xml = new DOMDocument();
$xml->load(‘cy.xml‘);
$main = $xml->getElementsByTagName_r(‘main‘);
foreach( $main as $main)
{
$list = $main->getElementsByTagName_r( "list" );
foreach ( $list as $list )
{
$value =
$list->firstChild->nodeValue;
echo $value."<br />";
}
$title = $main->getElementsByTagName_r( "title"
);
foreach ( $title as $title )
{
$value =
$title->firstChild->nodeValue;
echo $value."<br />";
}
}
$m = $xml->getElementsByTagName_r(‘m‘);
foreach( $m as $m)
{
$list = $m->getElementsByTagName_r( "list" );
foreach ( $list as $list )
{
$value =
$list->firstChild->nodeValue;
echo $value."<br />";
}
}
?>
cy.php输出结果:
1
2
3
a
4
5
6
b
7
8
9
想一下,如何读出<m>中<title></title>的值?
Php读取XML再通过一个例子巩固一下:
XML文件(cy.xml)如下:
<?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>
Php文件(cy.php)如下:
<?php
$xml = new DOMDocument(); //建立一个DOMDocument
$xml->load(‘cy.xml‘); //Php指定需要读取xml文件的位置
$LevelOne =
$xml->getElementsByTagName_r(‘LevelOne‘);//按照名称取得节点,返回所有节点的集合,不过这里这样读LevelOne是没有意义的....
$LevelOne =
$xml->getElementsByTagName_r(‘LevelOne‘)->item(0);//返回第一个LevelOne节点中的内容
$LevelTwo =
$LevelOne->getElementsByTagName_r(‘LevelTwo‘);
//按照名称取得节点,返回所有LevelTwo
foreach ( $LevelTwo as $Content
)//循环读出所有LevelTwo,并在循环里,把LevelTwo用Content表示
{
$LevelThree =
$Content->getElementsByTagName_r(‘LevelThree‘);//返回所有LevelThree
foreach ( $LevelThree as $Concert )
{
$name = $Concert->nodeName;//节点名称
$value = $Concert->nodeValue;//节点值
$id = $Concert->getAttribute(‘id‘);//"id"属性值
echo $name."<br />";
echo $value."<br />";
echo $id."<br />";
}
}
?>
如果用$LevelOne =
$xml->getElementsByTagName_r(‘LevelOne‘)这样的方式取得节点,那么,读取里面的内容,需要用foreach
循环,因为$LevelOne = $xml->getElementsByTagName
(‘LevelOne‘)返回的,是一个集合,并不是一个具体的节点----仅管叫做LevelOne的节点只有一个....
如果用$LevelOne =
$xml->getElementsByTagName_r(‘LevelOne‘)->item(0)这样的方式取得节点,那么读取里面的内
容,可以直接$LevelOne->xxxxxx,因为这样返回的是一个具体的节点。
现在提供一种很简便的方法用PHP读取XML:
XML文件(cy.xml)如下:
<?xml version="1.0"
encoding="gb2312"?>
<xml>
<site>
<part id="1">
<title id="a">czbin
xml版块</title>
<describe>xml的相关文章</describe>
</part>
<part id="2">
<title id="b">czbin
php版块</title>
<describe>php的相关文章</describe>
</part>
<part id="3">
<title id="c">czbin
ajax版块</title>
<describe>ajax的相关文章</describe>
</part>
</site>
</xml>
Php文件(cy.php)如下:
<?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输出结果:
1
czbin xml版块
a
xml的相关文章
2
czbin php版块
b
php的相关文章
3
czbin ajax版块
c
ajax的相关文章
怎么样?的确很简单吧!