0

这是我的示例 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>
4

3 回答 3

0

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

foreach($xml->ALL_VERSIONS->版本为 $child)
{

    echo "版本代码:".$child['code'];
    echo "版本名称:".$child['name'];

    foreach($child->RELEASES_LIST->发布为 $releaseChild)
    {
        echo "发布代码:".$releaseChild['code'];
        echo "发布名称:".$releaseChild['name'];

        echo "品牌代码:".$releaseChild->BRAND_LIST->BRAND['code'];
        echo "品牌名称:".$releaseChild->BRAND_LIST->BRAND['name'];
    }

}


于 2013-02-12T11:38:47.650 回答
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
                        )

                )

        )

)
*/
?>
于 2013-02-12T05:31:38.040 回答
-1

在 php 中你可以使用 DOMDocument

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

在 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

于 2013-02-12T05:08:28.440 回答