2013-02-12 82 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']; 
    } 

}