duansha7453 2011-03-11 01:01
浏览 188
已采纳

SimpleXML节点,节点名称中包含空格和等号

I have an xml document that contains spaces and equal signs in the node names. I'm trying to use SimpleXML to extract the data from those nodes but it returns blank no matter what I try.

A sample of the xml document

  <code><away>
  <radio>url.here</radio> 
  <live bitrate="1">url.here</live> 
  <live bitrate="0">url.here</live> 
  </away></code>

I have tried using bothecho "<td>".$node->away->{'live bitrate="1"'}."</td>"; echo "<td>".$node->away->{'live'}->{'bitrate="1"'}."</td>";

  • 写回答

1条回答 默认 最新

  • dongmi4035 2011-03-11 02:08
    关注

    Here is a function I use to convert the SimpleXML object to an array:

            public 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 = $this->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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型