book.xml
 <?xml version="1.0" encoding="GB2312" ?> 
- <air_info ret_value="1" date="21DEC(SUN)" from="SHA" to="PEK" fromname="上海虹桥" toname="北京首都" Mileage="1178">
- <airline line_number="CA1858" company="中国国际航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0745" atime="1005" airrax="50" fulerax="150" food="1" dfly="1" type="330" et="1">
  <c b="F" d="头等舱" p="1700" s="A" /> 
  <c b="Y" d="全价舱" p="1130" s="A" /> 
  <c b="S" d="*未知" p="0" s="A" /> 
  <c b="W" d="*未知" p="0" s="A" /> 
  <c b="T" d="*未知" p="0" s="A" /> 
  </airline>
- <airline line_number="*FM9891" company="中国上海航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0745" atime="1005" airrax="50" fulerax="150" food="1" dfly="1" type="330" et="1">
  <c b="F" d="头等舱" p="1700" s="A" /> 
  <c b="Y" d="全价舱" p="1130" s="A" /> 
  </airline>
- <airline line_number="MU5101" company="中国东方航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0800" atime="1020" airrax="50" fulerax="150" food="1" dfly="1" type="333" et="1">
  <c b="F" d="头等舱" p="1700" s="A" /> 
  <c b="A" d="*未知" p="0" s="5" /> 
  <c b="Y" d="全价舱" p="1130" s="A" /> 
  <c b="K" d="*未知" p="0" s="A" /> 
  </airline>
 </air_info>
pHP解析上面的xml航班的参数

解决方案 »

  1.   

    <?php
    $doc = new DOMDocument; 
    $doc->load('book.xml'); 
    $book = $doc->documentElement; 
    $airline = $book->getElementsByTagName('airline'); 
    //echo $air_info->getAttribute('ret_value');
    foreach($airline as $c) 

      echo $c->getAttribute('line_number'),'<br/>';
      //echo $c->getAttribute('company'),'<br/>';
      /* 其它属性 */
      $childnodes = $c->getElementsByTagName('c');
      foreach($childnodes as $childc){
       echo $childc->getAttribute('b'),'<br/>';
       /* 其它属性 $childc->getAttribute('d')*/
      }
    }
    ?>
      

  2.   

    CA1858 
    涓浗鍥介檯鑸┖鍏徃 
    SHA 





    *FM9891 
    涓浗涓婃捣鑸┖鍏徃 
    SHA 


    MU5101 
    涓浗涓滄柟鑸┖鍏徃 
    SHA 




    结果是不能识别GB2312,该怎么出来这个字符编码?
      

  3.   

    echo iconv( "UTF-8","GBK" , $c->getAttribute('company')),'<br/>';
      

  4.   


    你的xml指定的编码是gb2312,你的浏览器可能不是这种编码
      

  5.   

    首先确认你的网页是什么编码?如果是utf-8的话,那么你要转换xml成为utf-8 或者解析完成后把数据转为utf-8编码的
      

  6.   

    那我现在也顺便问下用php做网站,是用gb2312好呢,还是UTF-8好呢,新手学习!请高手赐教……
      

  7.   

    utf 字符集大 gb2312 字符集比较小,或者会碰到一些问题。
      

  8.   

    当然首选utf-8
    只要你服务器支持
      

  9.   

    用utf-8
    在你输出时候用iconv函数转换一下,如:
    echo iconv( "UTF-8","GBK" , $c->getAttribute('company')),' <br/>';