php 读取xml文件

返回xml数据
http://hi.baidu.com/jimofengfan/blog/item/31b77ecdeb075c5c0fb3458d.html

http://hi.baidu.com/aaxh/blog/item/026c78ec551bfad52e2e2107.html
简单读取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
$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 />";
}
?>

输出结果:
1
czbin xml版块
a
xml的相关文章
2
czbin php版块
b
php的相关文章
3
czbin ajax版块
c
ajax的相关文章


DOM读取
<?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
$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 />";
	}
}
?>


<?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
$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."<br />";
		echo $value."<br />";
		echo $id."<br />";
	}
}
?>

$main=$doc->getElementsByTagName('main'); 返回的是一个集合 需要foreach遍历
$main=$doc->getElementsByTagName('main')->item(0) 返回的是一个具体的节点 如果有多个<main></main>返回第一个
$name=$title->nodeName; //节点名 title、list等
$value=$title->nodeValue; //节点值

http://www.ibm.com/developerworks/cn/opensource/os-xmldomphp/
<books>
  <book>
  <author>Jack Herrington</author>
  <title>PHP Hacks</title>
  <publisher>O'Reilly</publisher>
  </book>
  <book>
  <author>Jack Herrington</author>
  <title>Podcasting Hacks</title>
  <publisher>O'Reilly</publisher>
  </book>
  </books>

<?php
  $doc = new DOMDocument();
  $doc->load( 'books.xml' );
  
  $books = $doc->getElementsByTagName( "book" );
  foreach( $books as $book )
  {
  $authors = $book->getElementsByTagName( "author" );
  $author = $authors->item(0)->nodeValue;
  
  $publishers = $book->getElementsByTagName( "publisher" );
  $publisher = $publishers->item(0)->nodeValue;
  
  $titles = $book->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;
  
  echo "$title - $author - $publisher\n";
  }
  ?>

你可能感兴趣的:(读取xml)