|
简单读取XML的方法
Java代码
- <?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>
Java代码
- <?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 />";
- }
- ?>
输出结果:
Java代码
- 1
- czbin xml版块
- a
- xml的相关文章
- 2
- czbin php版块
- b
- php的相关文章
- 3
- czbin ajax版块
- c
- ajax的相关文章
DOM读取
Java代码
- <?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>
Java代码
- <?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 />";
- }
- }
- ?>
Java代码
- <?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>
Java代码
- <?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/
Java代码
- <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>
Java代码
- <?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 Parser对XML文档进行解析的过程:
Php代码
- <?php
- // 处理开始元素函数
- function startElementHandler($parser, $element, $attributes) {
- echo "元素开始:".$element."<br>";
- if($attributes) {
- echo "属性:";
- foreach ( $attributes as $key => $value ) {
- echo $key."=".$value." ";
- }
- echo "<br>";
- }
- }
-
- // 处理结束元素函数
- function endElementHandler($parser, $element) {
- echo "元素结束:".$element."<br><br>";
- }
-
- // 处理字符串数据函数
- function characterDataHandler($parser, $data) {
- if(trim($data)) {
- echo "字符串数据:".htmlspecialchars($data)."<br>";
- }
- }
-
- // 处理解析错误函数
- function parserError($parser) {
- $code = xml_get_error_code($parser);
- $error = xml_error_string($code);
- $errorColumn = xml_get_current_column_number($parser);
- $errorLine = xml_get_current_line_number($parser);
- return "错误代码:".$code." 错误:".$error."在第".$errorLine."行第".$errorColumn."列";
- }
-
- // 创建解析器
- $parser = xml_parser_create();
-
- // 注册元素处理函数
- xml_set_element_handler($parser, "startElementHandler", "endElementHandler");
- xml_set_character_data_handler($parser, "characterDataHandler");
-
- // 获取文件内容
- $xml = file_get_contents("parser.xml");
-
- // 开始解析parser.xml文档,解析错误就调用错误处理函数
- xml_parse($parser, $xml) or die(parserError());
-
- // 删除解析器并释放内存
- xml_parser_free($parser);
-
- ?>
我们给定的XML示例是:
Xml代码
- <?xml version="1.0" encoding="UTF-8"?>
- <phpedu>
- <title>PHP100</title>
- <item type="text" name="item">
- <contents type="text">PHP</contents>
- <contents>SQL</contents>
- <contents>Linux</contents>
- <contents>Apache</contents>
- </item>
- <address type="URL">www.php100.com</address>
- </phpedu>
多说一句:希望对您有所帮助!不同语言对XML的解析区别于形式,统一于本质!:)
|
|
|