【问题标题】:How to count XML elements [duplicate]如何计算 XML 元素 [重复]
【发布时间】:2011-08-16 08:29:48
【问题描述】:

可能重复:
php count xml elements

我有一个包含视频标题的 XML 文件

<videolist>
<video>
<title>Title1</title>
</video>

<video>
<title>Title2</title>
</video>
</videolist>

如何统计视频元素的数量?我通常使用 simpleXML 来读取 XML 文件。在上面的例子中,有几个视频元素。

【问题讨论】:

    标签: php xml


    【解决方案1】:
    $s = simplexml_import_dom($dom);
    echo count($s -> video);
    

    【讨论】:

    • 我真的需要使用dom节点吗?如果我使用 simplexml_load_file("xml") 打开 xml 文件怎么样?
    【解决方案2】:

    以下是对 SimpleXML 对象的“视频”属性进行 count() 处理的示例。

    我还包含一个函数,用于将 simpleXML 对象转换为您也可以 count() 的数组。

    <?php
    
    $xml = '
    <videolist>
    <video>
    <title>Title1</title>
    </video>
    
    <video>
    <title>Title2</title>
    </video>
    </videolist>
    ';
    
    $sxe = new SimpleXMLElement($xml);
    
    print count($sxe->video)."\n";
    
    $array = simpleXMLToArray($sxe);
    
    print count($array['video'])."\n";
    
    function simpleXMLToArray($xml,
                            $flattenValues=true,
                            $flattenAttributes = true,
                            $flattenChildren=true,
                            $valueKey='@value',
                            $attributesKey='@attributes',
                            $childrenKey='@children'){
    
                    $return = array();
                    if(!($xml instanceof SimpleXMLElement)){return $return;}
                    $name = $xml->getName();
                    $_value = trim((string)$xml);
                    if(strlen($_value)==0){$_value = null;};
    
                    if($_value!==null){
                            if(!$flattenValues){$return[$valueKey] = $_value;}
                            else{$return = $_value;}
                    }
    
                    $children = array();
                    $first = true;
                    foreach($xml->children() as $elementName => $child){
                            $value = simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey);
                            if(isset($children[$elementName])){
                                    if($first){
                                            $temp = $children[$elementName];
                                            unset($children[$elementName]);
                                            $children[$elementName][] = $temp;
                                            $first=false;
                                    }
                                    $children[$elementName][] = $value;
                            }
                            else{
                                    $children[$elementName] = $value;
                            }
                    }
                    if(count($children)>0){
                            if(!$flattenChildren){$return[$childrenKey] = $children;}
                            else{$return = array_merge($return,$children);}
                    }
    
                    $attributes = array();
                    foreach($xml->attributes() as $name=>$value){
                            $attributes[$name] = trim($value);
                    }
                    if(count($attributes)>0){
                            if(!$flattenAttributes){$return[$attributesKey] = $attributes;}
                            else{$return = array_merge($return, $attributes);}
                    }
    
                    return $return;
            }
    

    【讨论】:

      【解决方案3】:

      您可以使用仅输出一些文本的简单 XSLT 脚本。 可以查看PHP provides for XSLT的各种功能

      一个简单的脚本看起来像这样:

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="text" encoding="iso-8859-1" indent="no"/>
       <xsl:template match="/">
        <xsl:value-of select="count(/videolist/video)" />
       </xsl:template>
      </xsl:stylesheet>
      

      要加载它,您的脚本可能如下所示:

      <?php
      
       $yourXMLData = '<videolist>...</videolist>';
       $XSLTScript = '<xsl:stylesheet ... ';
      
       // Load the XML source
       $xml = new DOMDocument;
       $xml->load($yourXMLData);
      
       $xsl = new DOMDocument;
       $xsl->load($XSLTScript);
      
       // Configure the transformer
       $proc = new XSLTProcessor;
       $proc->importStyleSheet($xsl); // attach the xsl rules
      
       echo $proc->transformToXML($xml);
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2017-08-23
        • 2020-10-06
        • 2017-11-06
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-08
        相关资源
        最近更新 更多