2013-02-12 126 views
0

這是我的示例xml文件。我如何獲得每個元素,屬性和它們的值。我想要所有版本的代碼和名稱,所有版本的代碼和名稱以及所有品牌的代碼和名稱?如何從xml獲取元素,屬性及其值?

<?xml version="1.0" encoding="UTF-8"?> 
<PLM_University namespace="DS"> 
    <ALL_VERSIONS> 
    <version code="1" name="V1" description="Version for CLS Products"> 
     <RELEASES_LIST> 
     <release code="7" name=".7" rank="5" description=""> 
      <BRAND_LIST> 
      <BRAND code="WLS" name="Companion"> 
       <SOLUTIONS_LIST> 
       <SOLUTION code="WLS_STU" name="Companion Learning Space" /> 

       </SOLUTIONS_LIST> 
      </BRAND> 
      </BRAND_LIST> 
     </release> 
     </RELEASES_LIST> 
    </version> 
    <version code="10" name="Matrix10" description="Version for Matrix Products"> 
     <RELEASES_LIST> 
     <release code="6" name=".6" rank="1" description=""> 
      <BRAND_LIST> 
      <BRAND code="ENOVIA" name="ENOVIA Collaborative Innovation"> 
       <SOLUTIONS_LIST> 
       <SOLUTION code="ENOV_MX1" name="ENOVIA MatrixOne" /> 

       </SOLUTIONS_LIST> 
      </BRAND> 
      </BRAND_LIST> 
      </release> 
     <release code="7" name=".7" rank="2" description=""> 
      <BRAND_LIST> 
      <BRAND code="ENOVIA" name="ENOVIA Collaborative Innovation"> 
       <SOLUTIONS_LIST> 
       <SOLUTION code="ENOV_MX1" name="ENOVIA MatrixOne" /> 

       </SOLUTIONS_LIST> 
      </BRAND> 
      </BRAND_LIST> 
     </release> 
      <release code="8" name=".8" rank="3" description=""> 
      <BRAND_LIST> 
      <BRAND code="ENOVIA" name="ENOVIA Collaborative Innovation"> 
       <SOLUTIONS_LIST> 
      <SOLUTION code="ENOV_MX1" name="ENOVIA MatrixOne" /> 
       </SOLUTIONS_LIST> 
      </BRAND> 
      </BRAND_LIST> 
     </release> 
    </RELEASES_LIST> 
    </version> 
</ALL_VERSIONS> 
</PLM_University> 

回答

-1

在PHP中,你可以在PHP

使用的DOMDocument

http://php.net/manual/en/class.domdocument.php

例如:

<?php 
    $handle = fopen("1.xml", "rb"); 
    $xml= ''; 
    while (!feof($handle)) { 
     $xml .= fread($handle, 8192); 
    } 
    fclose($handle); 

    $dom = new DOMDocument; 
    $dom->loadXML($xml); 
    $books = $dom->getElementsByTagName('book'); 
    foreach ($books as $book) { 
     echo $book->nodeValue, PHP_EOL; 
    } 
    ?> 

看到這個頁面瞭解更多例如http://www.php.net/manual/en/domdocument.getelementsbytagname.php

+0

我使用的文件,而不是字符串。你能否告訴我其他方式來做到這一點? – Alpa 2013-02-12 05:34:04

+0

@Alpa你可以使用fopen和fread從file.see中創建一個字符串。更改答案 – 2013-02-12 05:38:08

+0

@mohammadmohsenipur你不需要構建$ xml字符串,你可以簡單地使用'$ doc-> load('1.xml'); – 2013-02-12 05:40:25

0

這裏有一個簡單的解決方案,你可以工作:

<?php 
//Load xml file and than json encode/decode it into an array. 
$a = json_decode(json_encode((array) simplexml_load_file('path_to_your_xml.xml'),1); 

//Loop the array creating a return array 
$return = array(); 
foreach($a['ALL_VERSIONS']['version'] as $key=>$value){ 
    $return[$key]['code'] = $value['@attributes']['code']; 
    $return[$key]['name'] = $value['@attributes']['name']; 

    if(is_array($value['RELEASES_LIST']['release']) && isset($value['RELEASES_LIST']['release'][0]['BRAND_LIST']['BRAND']['@attributes']['code'])){ 
     //Loop sup versions 
     foreach($value['RELEASES_LIST']['release'] as $key2=>$sub){ 
      $return[$key][$key2]['RELEASES_LIST'] = array(
              'core'=>$sub['@attributes']['code'], 
              'name'=>$sub['@attributes']['name']); 
      $return[$key][$key2]['BRAND_LIST'] = array(
              'brand'=>$sub['BRAND_LIST']['BRAND']['@attributes']['code'], 
              'brand_name'=>$sub['BRAND_LIST']['BRAND']['@attributes']['name']); 
     } 

    }else{ 
    $return[$key]['RELEASES_LIST'] = array(
              'core'=>$value['RELEASES_LIST']['release']['@attributes']['code'], 
              'name'=>$value['RELEASES_LIST']['release']['@attributes']['name']); 
    $return[$key]['BRAND_LIST'] = array(
              'brand'=>$value['RELEASES_LIST']['release']['BRAND_LIST']['BRAND']['@attributes']['code'], 
              'brand_name'=>$value['RELEASES_LIST']['release']['BRAND_LIST']['BRAND']['@attributes']['name']); 
    } 
} 

print_r($return); 
/* 
Array 
(
    [0] => Array 
     (
      [code] => 1 
      [name] => V1 
      [RELEASES_LIST] => Array 
       (
        [core] => 7 
        [name] => .7 
       ) 

      [BRAND_LIST] => Array 
       (
        [brand] => WLS 
        [brand_name] => Companion 
       ) 

     ) 

    [1] => Array 
     (
      [code] => 10 
      [name] => Matrix10 
      [0] => Array 
       (
        [RELEASES_LIST] => Array 
         (
          [core] => 6 
          [name] => .6 
         ) 

        [BRAND_LIST] => Array 
         (
          [brand] => ENOVIA 
          [brand_name] => ENOVIA Collaborative Innovation 
         ) 

       ) 

      [1] => Array 
       (
        [RELEASES_LIST] => Array 
         (
          [core] => 7 
          [name] => .7 
         ) 

        [BRAND_LIST] => Array 
         (
          [brand] => ENOVIA 
          [brand_name] => ENOVIA Collaborative Innovation 
         ) 

       ) 

      [2] => Array 
       (
        [RELEASES_LIST] => Array 
         (
          [core] => 8 
          [name] => .8 
         ) 

        [BRAND_LIST] => Array 
         (
          [brand] => ENOVIA 
          [brand_name] => ENOVIA Collaborative Innovation 
         ) 

       ) 

     ) 

) 
*/ 
?> 
0
 


$xml = simplexml_load_file("data.xml"); 

foreach($xml->ALL_VERSIONS->version as $child) 
{ 

    echo "Version code : ".$child['code']; 
    echo "Version name : ".$child['name']; 

    foreach($child->RELEASES_LIST->release as $releaseChild) 
    { 
     echo "release code : ".$releaseChild['code']; 
     echo "release name : ".$releaseChild['name']; 

     echo "brand code : ".$releaseChild->BRAND_LIST->BRAND['code']; 
     echo "brand name : ".$releaseChild->BRAND_LIST->BRAND['name']; 
    } 

} 


相關問題