SimpleXML/PHP - 无法访问对象

发布于 2024-12-02 05:11:48 字数 808 浏览 1 评论 0原文

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = simplexml_load_string($tmp2);
echo $xml->RESPONSE->DATA->HEADER->COLUMN[0];

即使 var_dump 成功,上面也不会输出任何内容:

object(SimpleXMLElement)#2 (1) {
  ["RESPONSE"]=>
  object(SimpleXMLElement)#3 (1) {
    ["DATA"]=>
    object(SimpleXMLElement)#4 (1) {
      ["HEADER"]=>
      object(SimpleXMLElement)#5 (1) {
        ["COLUMN"]=>
        array(2) {
          [0]=>
          string(8) "interval"
          [1]=>
          string(13) "creative_name"
        }
      }
    }
  }
}

谢谢

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = simplexml_load_string($tmp2);
echo $xml->RESPONSE->DATA->HEADER->COLUMN[0];

The above won't output anything, even though a var_dump is sucessful:

object(SimpleXMLElement)#2 (1) {
  ["RESPONSE"]=>
  object(SimpleXMLElement)#3 (1) {
    ["DATA"]=>
    object(SimpleXMLElement)#4 (1) {
      ["HEADER"]=>
      object(SimpleXMLElement)#5 (1) {
        ["COLUMN"]=>
        array(2) {
          [0]=>
          string(8) "interval"
          [1]=>
          string(13) "creative_name"
        }
      }
    }
  }
}

Thanks

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

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

发布评论

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

评论(5

影子是时光的心 2024-12-09 05:11:48

这是因为 SimpleXML 需要精确的类型转换,否则您会遇到像这样的荒谬的事情 - var_dump 将输出您想要的内容,而 echo 不会。你总是需要这样做,还有更糟糕的错误,比如echo ceil($simplexml->someNumber)将输出7,如果数字例如 7.85 等等。
尝试改为:

echo (string)$xml->RESPONSE->DATA->HEADER->COLUMN[0];

This is because SimpleXML requires exact typecasting or you'll get riddiculous things happening like this - var_dump will output what you want, echo won't. You always need to do this, there are even worse bugs, like echo ceil($simplexml->someNumber) will output 7 if the number is for example 7.85 and so on.
Try instead:

echo (string)$xml->RESPONSE->DATA->HEADER->COLUMN[0];
梦过后 2024-12-09 05:11:48

XML 输入有效,我想问题出在您的 PHP 设置(旧版本或有错误的版本)上。这是我的机器上的输出(PHP 5.3.8):

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = new SimpleXMLElement($tmp2);
var_dump($xml);

// output:
object(SimpleXMLElement)#1 (1) {
  ["RESPONSE"]=>
  object(SimpleXMLElement)#2 (1) {
    ["DATA"]=>
    object(SimpleXMLElement)#3 (1) {
      ["HEADER"]=>
      object(SimpleXMLElement)#4 (1) {
        ["COLUMN"]=>
        array(2) {
          [0]=>
          string(8) "interval"
          [1]=>
          string(4) "name"
        }
      }
    }
  }
}

问题更新后编辑

这确实必须是您的 PHP 版本,这又是我机器上的输出:

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = simplexml_load_string($tmp2);
echo $xml->RESPONSE->DATA->HEADER->COLUMN[0];

// output:
interval

The XML input is valid, I suppose the problem is with your PHP setup (old or buggy version). This is the output on my machine (PHP 5.3.8):

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = new SimpleXMLElement($tmp2);
var_dump($xml);

// output:
object(SimpleXMLElement)#1 (1) {
  ["RESPONSE"]=>
  object(SimpleXMLElement)#2 (1) {
    ["DATA"]=>
    object(SimpleXMLElement)#3 (1) {
      ["HEADER"]=>
      object(SimpleXMLElement)#4 (1) {
        ["COLUMN"]=>
        array(2) {
          [0]=>
          string(8) "interval"
          [1]=>
          string(4) "name"
        }
      }
    }
  }
}

EDIT after update of question:

This really must be your PHP version, this is the output on my machine again:

$tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';
$xml = simplexml_load_string($tmp2);
echo $xml->RESPONSE->DATA->HEADER->COLUMN[0];

// output:
interval
心如狂蝶 2024-12-09 05:11:48

您可以尝试抑制错误,然后迭代它们并检查是否可以修复它们,如解释的 此处,因为我在示例 XML 中看不到错误。

You could try to suppress the errors and then iterate over them an check if you can fix them, like explained HERE, because I can't see an error in the example XML.

风追烟花雨 2024-12-09 05:11:48
   $tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';

    $xml = simplexml_load_string($tmp2);
    var_dump($xml);
对象(SimpleXMLElement)#1 (1) {
  [“响应”]=>
  对象(SimpleXMLElement)#2 (1) {
    [“数据”]=>
    对象(SimpleXMLElement)#3 (1) {
      [“标题”] =>
      对象(SimpleXMLElement)#4 (1) {
        [“栏”]=>
        数组(2){
          [0] =>
          字符串(8)“间隔”
          [1]=>
          字符串(4)“名称”
        }
      }
    }
  }
}
   $tmp2 = '<?xml version="1.0"?><RWResponse><RESPONSE><DATA><HEADER><COLUMN>interval</COLUMN><COLUMN>name</COLUMN></HEADER></DATA></RESPONSE></RWResponse>';

    $xml = simplexml_load_string($tmp2);
    var_dump($xml);
object(SimpleXMLElement)#1 (1) {
  ["RESPONSE"]=>
  object(SimpleXMLElement)#2 (1) {
    ["DATA"]=>
    object(SimpleXMLElement)#3 (1) {
      ["HEADER"]=>
      object(SimpleXMLElement)#4 (1) {
        ["COLUMN"]=>
        array(2) {
          [0]=>
          string(8) "interval"
          [1]=>
          string(4) "name"
        }
      }
    }
  }
}
等待圉鍢 2024-12-09 05:11:48
$tmp2 = <<<XML<?xml version="1.0" standalone='yes'?>
<RWResponse>
 <RESPONSE>
  <DATA>
   <HEADER>
    <COLUMN>interval</COLUMN>
    <COLUMN>name</COLUMN>
   </HEADER>
  </DATA>
 </RESPONSE>
</RWResponse>
XML;

这将是真正的方法,因此无论 XML 输入是什么,您都可以对其进行配置,使其看起来更像这样,这样它就不是“字符串”,而是实际上制定的 XML 信息。

$tmp2 = <<<XML<?xml version="1.0" standalone='yes'?>
<RWResponse>
 <RESPONSE>
  <DATA>
   <HEADER>
    <COLUMN>interval</COLUMN>
    <COLUMN>name</COLUMN>
   </HEADER>
  </DATA>
 </RESPONSE>
</RWResponse>
XML;

would be the real way to do it, so whatever the XML input is, you would take and configure it so that it looks more like that so that it's not a 'string' but it's actually formulated XML information.

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