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

发布于 2024-10-21 02:39:08 字数 458 浏览 3 评论 0原文

我有一个 xml 文档,其中节点名称中包含空格和等号。我正在尝试使用 SimpleXML 从这些节点中提取数据,但无论我尝试什么,它都会返回空白。

示例

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

我尝试使用echo "".$node->away->{'live bitrate="1"'}.""; 的 xml 文档 echo "".$node->away->{'live'}->{'bitrate="1"'}."";

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>";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2024-10-28 02:39:08

这是我用来将 SimpleXML 对象转换为数组的函数:

        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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文