首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从xml中获取元素、属性及其值?

如何从xml中获取元素、属性及其值?
EN

Stack Overflow用户
提问于 2013-02-12 13:01:34
回答 2查看 229关注 0票数 0

下面是我的示例xml文件。如何获取每个元素、属性和它们的值。我想要所有版本的代码和名称,所有版本的代码和名称以及所有品牌的代码和名称?

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

发布于 2013-02-12 13:31:38

这里有一个你可以使用的简单的解决方案:

代码语言:javascript
复制
<?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
EN

Stack Overflow用户

发布于 2013-02-12 13:08:28

在php中你可以使用DOMDocument。

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

在php上

例如:

代码语言:javascript
复制
  <?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,请参阅此页面

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14825702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档