AJax技术

PHP读取XML格式文件的方法大全

字号+ 作者:H5之家 来源:H5之家 2017-03-03 12:05 我要评论( )

PHP读取XML格式文件的方法大全方法如下books.xml文件:JackHerringtonPHPHacks

PHP读取XML格式文件的方法大全方法如下


books.xml文件:

<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>

1.DOMDocument方法

<?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"; echo "<br>"; } ?>

2.用 SAX 解析器读取 XML:

<?php $g_books = array(); $g_elem = null; function startElement( $parser, $name, $attrs )  { global $g_books, $g_elem; if ( $name == 'BOOK' ) $g_books []= array(); $g_elem = $name; } function endElement( $parser, $name )  { global $g_elem; $g_elem = null; } function textData( $parser, $text ) { global $g_books, $g_elem; if ( $g_elem == 'AUTHOR' || $g_elem == 'PUBLISHER' || $g_elem == 'TITLE' ) { $g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text; } } $parser = xml_parser_create(); xml_set_element_handler( $parser, "startElement", "endElement" ); xml_set_character_data_handler( $parser, "textData" ); $f = fopen( 'books.xml', 'r' ); while( $data = fread( $f, 4096 ) ) { xml_parse( $parser, $data ); } xml_parser_free( $parser ); foreach( $g_books as $book ) { echo $book['TITLE']." - ".$book['AUTHOR']." - "; echo $book['PUBLISHER']."\n"; } ?>

3.用正则表达式解析 XML:

<?php $xml = ""; $f = fopen( 'books.xml', 'r' ); while( $data = fread( $f, 4096 ) ) {   $xml .= $data;  } fclose( $f ); preg_match_all( "/\<book\>(.*?)\<\/book\>/s", $xml, $bookblocks ); foreach( $bookblocks[1] as $block ) { preg_match_all( "/\<author\>(.*?)\<\/author\>/", $block, $author ); preg_match_all( "/\<title\>(.*?)\<\/title\>/",  $block, $title ); preg_match_all( "/\<publisher\>(.*?)\<\/publisher\>/", $block, $publisher ); echo( $title[1][0]." - ".$author[1][0]." - ".$publisher[1][0]."\n" ); } ?>

4.解析XML到数组

<?php   $data = "<root><line /><content language=\"gb2312\">简单的XML数据</content></root>";   $parser = xml_parser_create(); //创建解析器   xml_parse_into_struct($parser, $data, $values, $index); //解析到数组   xml_parser_free($parser); //释放资源   //显示数组结构   echo "\n索引数组\n";   print_r($index);   echo "\n数据数组\n";   print_r($values); ?>

5.检查XML是否有效

<?php   //创建XML解析器   $xml_parser = xml_parser_create();   //使用大小写折叠来保证能在元素数组中找到这些元素名称   xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);   //读取XML文件   $xmlfile = "bb.xml";   if (!($fp = fopen($xmlfile, "r")))   {     die("无法读取XML文件$xmlfile");   }   //解析XML文件   $has_error = false;      //标志位   while ($data = fread($fp, 4096))   {     //循环地读入XML文档,只到文档的EOF,同时停止解析     if (!xml_parse($xml_parser, $data, feof($fp)))     {       $has_error = true;       break;     }   }   if($has_error)   {      echo "该XML文档是错误的!<br />";     //输出错误行,列及其错误信息     $error_line  = xml_get_current_line_number($xml_parser);     $error_row  = xml_get_current_column_number($xml_parser);     $error_string = xml_error_string(xml_get_error_code($xml_parser));     $message = sprintf("[第%d行,%d列]:%s",              $error_line,             $error_row,             $error_string);     echo $message;   }   else   {     echo "该XML文档是结构良好的。";   }   //关闭XML解析器指针,释放资源   xml_parser_free($xml_parser); ?>

6.可用于精确的读取XML

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 如何基于Ajax技术实现文件上传带进度条?

    如何基于Ajax技术实现文件上传带进度条?

    2017-02-07 12:02

  • Js原生Ajax传递Json格式数据

    Js原生Ajax传递Json格式数据

    2017-01-28 10:01

  • highcharts图表高级入门之Tooltip:如何在Tooltip的格式化回调

    highcharts图表高级入门之Tooltip:如何在Tooltip的格式化回调

    2017-01-13 12:00

  • ajax实现文件上传 Ajax实现文件下载

    ajax实现文件上传 Ajax实现文件下载

    2017-01-11 17:00

网友点评