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

 

2.用 SAX 解析器读取 XML

 

代码
 1  <? php
 2  $g_books   =   array ();
 3  $g_elem   =   null ;
 4 
 5  function  startElement(  $parser ,   $name ,   $attrs  ) 
 6  {
 7  global   $g_books ,   $g_elem ;
 8  if  (  $name   ==   ' BOOK '  )  $g_books  [] =   array ();
 9  $g_elem   =   $name ;
10  }
11 
12  function  endElement(  $parser ,   $name  ) 
13  {
14  global   $g_elem ;
15  $g_elem   =   null ;
16  }
17 
18  function  textData(  $parser ,   $text  )
19  {
20  global   $g_books ,   $g_elem ;
21  if  (  $g_elem   ==   ' AUTHOR '   ||
22  $g_elem   ==   ' PUBLISHER '   ||
23  $g_elem   ==   ' TITLE '  )
24  {
25  $g_books count $g_books  )  -   1  ][  $g_elem  ]  =   $text ;
26  }
27  }
28 
29  $parser   =   xml_parser_create ();
30 
31  xml_set_element_handler $parser ,   " startElement " ,   " endElement "  );
32  xml_set_character_data_handler $parser ,   " textData "  );
33 
34  $f   =   fopen ' books.xml ' ,   ' r '  );
35 
36  while $data   =   fread $f ,   4096  ) )
37  {
38  xml_parse $parser ,   $data  );
39  }
40 
41  xml_parser_free $parser  );
42 
43  foreach $g_books   as   $book  )
44  {
45  echo   $book [ ' TITLE ' ] . "  -  " . $book [ ' AUTHOR ' ] . "  -  " ;
46  echo   $book [ ' PUBLISHER ' ] . " \n " ;
47  }
48  ?>

 

 

3.用正则表达式解析 XML

 

代码
 1  <? php
 2  $xml   =   "" ;
 3  $f   =   fopen ' books.xml ' ,   ' r '  );
 4  while $data   =   fread $f ,   4096  ) ) {
 5       $xml   .=   $data
 6  }
 7  fclose $f  );
 8 
 9  preg_match_all " /\<book\>(.*?)\<\/book\>/s " ,   $xml ,   $bookblocks  );
10 
11  foreach $bookblocks [ 1 as   $block  )
12  {
13  preg_match_all " /\<author\>(.*?)\<\/author\>/ " ,   $block ,   $author  );
14  preg_match_all " /\<title\>(.*?)\<\/title\>/ " ,     $block ,   $title  );
15  preg_match_all " /\<publisher\>(.*?)\<\/publisher\>/ " ,   $block ,   $publisher  );
16  echo $title [ 1 ][ 0 ] . "  -  " . $author [ 1 ][ 0 ] . "  -  " . $publisher [ 1 ][ 0 ] . " \n "  );
17  }
18  ?>

 

 

4.解析XML到数 组

 

代码
 1  <? php
 2       $data   =   " <root><line /><content language=\ " gb2312\ " >简单的XML数据</content></root> " ;
 3       $parser   =   xml_parser_create ();                         // 创建解析器
 4       xml_parse_into_struct ( $parser ,   $data ,   $values ,   $index );     // 解析到数组
 5       xml_parser_free ( $parser );                             // 释放资源
 6      
 7      //显示数组结构
 8       echo   " \n索引数组\n " ;
 9       print_r ( $index );
10       echo   " \n数据数组\n " ;
11       print_r ( $values );
12  ?>

 

5.检查XML是否有效

 

代码
 1  <? php
 2       // 创建XML解析器
 3       $xml_parser   =   xml_parser_create ();
 4 
 5       // 使用大小写折叠来保证能在元素数组中找到这些元素名称
 6       xml_parser_set_option ( $xml_parser ,  XML_OPTION_CASE_FOLDING ,   true );
 7 
 8       // 读取XML文件
 9       $xmlfile   =   " bb.xml " ;
10       if  ( ! ( $fp   =   fopen ( $xmlfile ,   " r " )))
11      {
12           die ( " 无法读取XML文件 $xmlfile " );
13      }
14 
15       // 解析XML文件
16       $has_error   =   false ;             // 标志位
17       while  ( $data   =   fread ( $fp ,   4096 ))
18      {
19           // 循环地读入XML文档,只到文档的EOF,同时停止解析
20           if  ( ! xml_parse ( $xml_parser ,   $data ,   feof ( $fp )))
21          {
22               $has_error   =   true ;
23               break ;
24          }
25      }
26 
27       if ( $has_error )
28      { 
29           echo   " 该XML文档是错误的!<br /> " ;
30 
31           // 输出错误行,列及其错误信息
32           $error_line     =   xml_get_current_line_number ( $xml_parser );
33           $error_row     =   xml_get_current_column_number ( $xml_parser );
34           $error_string   =   xml_error_string ( xml_get_error_code ( $xml_parser ));
35 
36           $message   =   sprintf ( " [第%d行,%d列]:%s " ,  
37                           $error_line ,
38                           $error_row ,
39                           $error_string );
40           echo   $message ;
41      }
42       else
43      {
44           echo   " 该XML文档是结构良好的。 " ;
45      }
46      
47       // 关闭XML解析器指针,释放资源
48       xml_parser_free ( $xml_parser );
49  ?>

 

 

6.可用于精确的读取XML
test.xml

代码
 1  <? xml version = " 1.0 "  encoding = " UTF-8 "   ?>
 2       < SBMP_MO_MESSAGE >
 3           < CONNECT_ID > 100 </ CONNECT_ID >
 4           < MO_MESSAGE_ID > 123456 </ MO_MESSAGE_ID >
 5           < RECEIVE_DATE > 20040605 </ RECEIVE_DATE >
 6           < RECEIVE_TIME > 153020 </ RECEIVE_TIME >
 7           < GATEWAY_ID > 1 </ GATEWAY_ID >
 8           < VALID > 1 </ VALID >
 9           < CITY_CODE > 010 </ CITY_CODE >
10           < CITY_NAME > 北京 </ CITY_NAME >
11           < STATE_CODE > 010 </ STATE_CODE >
12           < STATE_NAME > 北京 </ STATE_NAME >
13           < TP_PID > 0 </ TP_PID >
14           < TP_UDHI > 0 </ TP_UDHI >
15           < MSISDN > 15933626501 </ MSISDN >
16           < MESSAGE_TYPE > 8 </ MESSAGE_TYPE >
17           < MESSAGE > 5618常年供应苗木,品种有玉兰、黄叶杨等。联系人:张三,电话: 1234567890 </ MESSAGE >
18           < LONG_CODE > 100 </ LONG_CODE >
19           < SERVICE_CODE > 9588 </ SERVICE_CODE >
20       </ SBMP_MO_MESSAGE >
21  test . php :
22  <? php
23  $myData   =   array ();
24  $file   =   file_get_contents ( " test.xml " );
25  if ( strpos ( $file ,   ' <?xml ' >   - 1 ) {
26           try  {
27               // 加载解析xml
28               $xml   =   simplexml_load_string ( $file );
29               if ( $xml ) {
30                   // echo $this->result;
31                  //获取节点值
32                   $CONNECT_ID   =   $xml -> CONNECT_ID;
33                   $MO_MESSAGE_ID   =   $xml -> MO_MESSAGE_ID;
34                   $RECEIVE_DATE   =   $xml -> RECEIVE_DATE;
35                   $RECEIVE_TIME   =   $xml -> RECEIVE_TIME;
36                   $GATEWAY_ID   =   $xml -> GATEWAY_ID;
37                   $VALID   =   $xml -> VALID;
38                   $CITY_CODE   =   $xml -> CITY_CODE;
39                   $CITY_NAME   =   $xml -> CITY_NAME;
40                   $STATE_CODE   =   $xml -> CITY_CODE;
41                   $STATE_NAME   =   $xml -> STATE_NAME;
42                   $TP_PID   =   $xml -> TP_PID;
43                   $TP_UDHI   =   $xml -> TP_UDHI;
44                   $MSISDN   =   $xml -> MSISDN;
45                   $MESSAGE_TYPE   =   $xml -> MESSAGE_TYPE;
46                   $MESSAGE   =   $xml -> MESSAGE; // 短信
47                   $LONG_CODE   =   $xml -> LONG_CODE;
48                   $SERVICE_CODE   =   $xml -> SERVICE_CODE;
49                   preg_match ( " /(561)\d{1,2}/ " ,   $MESSAGE ,   $code );
50                   switch ( $code [ 0 ]) {
51                       case   5618   :
52                           $myData [message]  =   $MESSAGE ;
53                           break ;
54                       default   :
55                           $myData []  =   ' 没有短消息。 ' ;
56                           break ;
57                      }
58                      
59                  }  else  {
60                       echo   " 加载xml文件错误。 " ;
61                  }
62              
63          }  catch ( exception   $e ){
64               print_r ( $e );
65          }
66 
67  else  {
68       echo   " 没有该XML文件。 " ;
69  }
70 
71  echo   " <pre> " ;
72  print_r ( $myData );
73  echo   " <hr> " ;
74  echo   $myData [message];
75  ?>

 

 

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