`
这些年
  • 浏览: 388836 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

php xml(转)

    博客分类:
  • php
 
阅读更多
简单读取XML的方法 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="gb2312"?>  
  2. <xml>  
  3. <site>  
  4. <part id="1">  
  5. <title id="a">czbin xml版块</title>  
  6. <describe>xml的相关文章</describe>  
  7. </part>  
  8. <part id="2">  
  9. <title id="b">czbin php版块</title>  
  10. <describe>php的相关文章</describe>  
  11. </part>  
  12. <part id="3">  
  13. <title id="c">czbin ajax版块</title>  
  14. <describe>ajax的相关文章</describe>  
  15. </part>  
  16. </site>  
  17. </xml>  

Java代码   收藏代码
  1. <?php  
  2. $xml = simplexml_load_file('sxml.xml');  
  3. $part = $xml->site->part;  
  4. foreach ( $part as $content )  
  5. {  
  6. echo $content['id']."<br />";  
  7. echo $content->title."<br />";  
  8. echo $content->title['id']."<br />";  
  9. echo $content->describe."<br />";  
  10. }  
  11. ?>  

输出结果: 
Java代码   收藏代码
  1. 1  
  2. czbin xml版块  
  3. a  
  4. xml的相关文章  
  5. 2  
  6. czbin php版块  
  7. b  
  8. php的相关文章  
  9. 3  
  10. czbin ajax版块  
  11. c  
  12. ajax的相关文章  


DOM读取 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="gb2312"?>  
  2. <xml>  
  3. <main>  
  4. <list>1</list>  
  5. <list>2</list>  
  6. <list>3</list>  
  7. </main>  
  8. <main>  
  9. <list>4</list>  
  10. <list>5</list>  
  11. <list>6</list>  
  12. </main>  
  13. <m>  
  14. <list>7</list>  
  15. <list>8</list>  
  16. <list>9</list>  
  17. </m>  
  18. </xml>  

Java代码   收藏代码
  1. <?php  
  2. $xml = new DOMDocument();  
  3. $xml->load('cy.xml');  
  4. $main = $xml->getElementsByTagName('main');  
  5. foreach( $main as $main)  
  6. {  
  7.     $list = $main->getElementsByTagName( "list" );  
  8.     foreach ( $list as $list )  
  9.     {  
  10.         $value = $list->firstChild->nodeValue;  
  11.         echo $value."<br />";  
  12.     }  
  13. }  
  14. $m = $xml->getElementsByTagName('m');  
  15. foreach( $m as $m)  
  16. {  
  17.     $list = $m->getElementsByTagName( "list" );  
  18.     foreach ( $list as $list )  
  19.     {  
  20.         $value = $list->firstChild->nodeValue;  
  21.         echo $value."<br />";  
  22.     }  
  23. }  
  24. ?>  


Java代码   收藏代码
  1. <?xml version="1.0" encoding="gb2312"?>  
  2. <LevelOne>  
  3. <LevelTwo>  
  4. <LevelThree id="1">This is Text One</LevelThree>  
  5. <LevelThree id="2">This is Text Two</LevelThree>  
  6. <LevelThree id="3">This is Text Three</LevelThree>  
  7. </LevelTwo>  
  8. <LevelTwo>  
  9. <LevelThree id="4">This is Text Four</LevelThree>  
  10. <LevelThree id="5">This is Text Five</LevelThree>  
  11. <LevelThree id="6">This is Text Six</LevelThree>  
  12. </LevelTwo>  
  13. </LevelOne>  

Java代码   收藏代码
  1. <?php  
  2. $xml = new DOMDocument(); //建立一个DOMDocument  
  3. $xml->load('cy.xml'); //Php指定需要读取xml文件的位置  
  4. $LevelOne = $xml->getElementsByTagName('LevelOne');//按照名称取得节点,返回所有节点的集合,不过这里这样读LevelOne是没有意义的....  
  5. $LevelOne = $xml->getElementsByTagName('LevelOne')->item(0);//返回第一个LevelOne节点中的内容  
  6. $LevelTwo = $LevelOne->getElementsByTagName('LevelTwo'); //按照名称取得节点,返回所有LevelTwo  
  7. foreach ( $LevelTwo as $Content )//循环读出所有LevelTwo,并在循环里,把LevelTwo用Content表示  
  8. {  
  9.     $LevelThree = $Content->getElementsByTagName('LevelThree');//返回所有LevelThree  
  10.     foreach ( $LevelThree as $Concert )  
  11.     {  
  12.         $name = $Concert->nodeName;//节点名称  
  13.         $value = $Concert->nodeValue;//节点值  
  14.         $id = $Concert->getAttribute('id');//"id"属性值  
  15.         echo $name."<br />";  
  16.         echo $value."<br />";  
  17.         echo $id."<br />";  
  18.     }  
  19. }  
  20. ?>  

$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代码   收藏代码
  1. <books>  
  2.   <book>  
  3.   <author>Jack Herrington</author>  
  4.   <title>PHP Hacks</title>  
  5.   <publisher>O'Reilly</publisher>  
  6.   </book>  
  7.   <book>  
  8.   <author>Jack Herrington</author>  
  9.   <title>Podcasting Hacks</title>  
  10.   <publisher>O'Reilly</publisher>  
  11.   </book>  
  12.   </books>  

Java代码   收藏代码
  1. <?php  
  2.   $doc = new DOMDocument();  
  3.   $doc->load( 'books.xml' );  
  4.     
  5.   $books = $doc->getElementsByTagName( "book" );  
  6.   foreach( $books as $book )  
  7.   {  
  8.   $authors = $book->getElementsByTagName( "author" );  
  9.   $author = $authors->item(0)->nodeValue;  
  10.     
  11.   $publishers = $book->getElementsByTagName( "publisher" );  
  12.   $publisher = $publishers->item(0)->nodeValue;  
  13.     
  14.   $titles = $book->getElementsByTagName( "title" );  
  15.   $title = $titles->item(0)->nodeValue;  
  16.     
  17.   echo "$title - $author - $publisher\n";  
  18.   }  
  19.   ?>  

  对数据进行解析是应用开发必不可少的一个环节。下面是使用XML Parser对XML文档进行解析的过程:

Php代码  收藏代码
  1. <?php  
  2. // 处理开始元素函数  
  3. function startElementHandler($parser$element$attributes) {  
  4.     echo "元素开始:".$element."<br>";  
  5.     if($attributes) {  
  6.         echo "属性:";  
  7.         foreach ( $attributes as $key => $value ) {  
  8.             echo $key."=".$value." ";  
  9.         }  
  10.         echo "<br>";  
  11.     }  
  12. }  
  13.   
  14. // 处理结束元素函数  
  15. function endElementHandler($parser$element) {  
  16.     echo "元素结束:".$element."<br><br>";  
  17. }  
  18.   
  19. // 处理字符串数据函数  
  20. function characterDataHandler($parser$data) {  
  21.     if(trim($data)) {  
  22.         echo "字符串数据:".htmlspecialchars($data)."<br>";  
  23.     }  
  24. }  
  25.   
  26. // 处理解析错误函数  
  27. function parserError($parser) {  
  28.     $code = xml_get_error_code($parser);  
  29.     $error = xml_error_string($code);  
  30.     $errorColumn = xml_get_current_column_number($parser);  
  31.     $errorLine = xml_get_current_line_number($parser);  
  32.     return "错误代码:".$code." 错误:".$error."在第".$errorLine."行第".$errorColumn."列";  
  33. }  
  34.   
  35. // 创建解析器  
  36. $parser = xml_parser_create();  
  37.   
  38. // 注册元素处理函数  
  39. xml_set_element_handler($parser"startElementHandler""endElementHandler");  
  40. xml_set_character_data_handler($parser"characterDataHandler");  
  41.   
  42. // 获取文件内容  
  43. $xml = file_get_contents("parser.xml");  
  44.   
  45. // 开始解析parser.xml文档,解析错误就调用错误处理函数  
  46. xml_parse($parser$xmlor die(parserError());  
  47.   
  48. // 删除解析器并释放内存  
  49. xml_parser_free($parser);  
  50.   
  51. ?>  

    我们给定的XML示例是:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <phpedu>  
  3.     <title>PHP100</title>  
  4.     <item type="text" name="item">  
  5.         <contents type="text">PHP</contents>  
  6.         <contents>SQL</contents>  
  7.         <contents>Linux</contents>  
  8.         <contents>Apache</contents>  
  9.     </item>  
  10.     <address type="URL">www.php100.com</address>  
  11. </phpedu>  

    多说一句:希望对您有所帮助!不同语言对XML的解析区别于形式,统一于本质!:)

 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics