<?xml version="1.0" encoding="GB2312" ?>
<air_info>
<airline atime="1020" airrax="50" fulerax="0" food="1" dfly="1"> 
<c b="F" d="100" p="1700" s="A" /> 
  <c b="Y" d="100" p="1130" s="A" /> 
<c b="B" d="80" p="1020" s="A" /> 
  <c b="H" d="80" p="900" s="A" /> 
  <c b="L" d="75" p="850" s="A" /> 
  <c b="M" d="70" p="790" s="A" /> 
</airline>
</air_info>
book.xml文件问题: 
1.我怎么通过foreach解析里面的属性(用DOM方法解析); 
2.解析出来以后我想要排序p的值,是从小-大排序; 
如: 
790 
850 
900 
1020 
1130 
1700 
请高手赐教…………………… 

解决方案 »

  1.   

    问题: 
    1.我怎么通过foreach或者FOR循序解析里面的属性(用DOM方法解析); 
    2.解析出来以后我想要排序p的值,是从小-大排序; 
    如: 
    790 
    850 
    900 
    1020 
    1130 
    1700 
      

  2.   


    <?php
     $doc = new DOMDocument; //xml开始解析
    $doc->load('book.xml');
    $book=$doc->documentElement; 
    $airline =$book->getElementsByTagName('airline'); 
     
    foreach($airline as $c) 

        $childnodes = $c->getElementsByTagName('c'); 
     foreach($childnodes as $childc)
      { 
      $p=$childc->getAttribute('p');
      echo $p."<br>";
      }
    }  ?>我这个结果是:1700
    1130
    1020
    900
    850
    790
    我相信让这个数字从小-大排序,如:790 
    850 
    900 
    1020 
    1130 
    1700 
    怎么实现?
      

  3.   


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

        $childnodes = $c->getElementsByTagName('c'); 
        $arr = array();
        $ListP = array();
        $i = 0;
        foreach($childnodes as $childc)
        { 
          $list = array();
          $ListP[$i] = $childc->getAttribute('p');
          $list[0] = $childc->getAttribute('b'); 
          $list[1] = $childc->getAttribute('d');
          $list[2] = $childc->getAttribute('p');
          $list[3] = $childc->getAttribute('s');
          $arr[$i] = $list;
          $i++;
        }
    }
    sort($ListP);
    for($i = 0;$i < count($ListP);$i++){
        for($j = 0;$j < count($arr);$j++){
            $b = $arr[$j][0];
            $d = $arr[$j][1];
            $p = $arr[$j][2];
            $s = $arr[$j][3];
            if($ListP[$i] == $p){
                echo $p."|".$b."|". $d."|". $p."|".$s."<br>";
            }
        }
    }
    ?>
      

  4.   


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

        $childnodes = $c->getElementsByTagName('c'); 
        $arr = array();
        $ListP = array();
        $i = 0;
        foreach($childnodes as $childc)
        { 
          $list = array();
          $ListP[$i] = $childc->getAttribute('p');
          $list[0] = $childc->getAttribute('b'); 
          $list[1] = $childc->getAttribute('d');
          $list[2] = $childc->getAttribute('p');
          $list[3] = $childc->getAttribute('s');
          $arr[$i] = $list;
          $i++;
        }
    }
    sort($ListP);
    for($i = 0;$i < count($ListP);$i++){
        for($j = 0;$j < count($arr);$j++){
            $b = $arr[$j][0];
            $d = $arr[$j][1];
            $p = $arr[$j][2];
            $s = $arr[$j][3];
            if($ListP[$i] == $p){
                echo $p."|".$b."|". $d."|". $p."|".$s."<br>";
            }
        }
    }
    ?>