【问题标题】:Retrieve and XML element and store as a variable检索和 XML 元素并存储为变量
【发布时间】:2012-11-16 15:42:01
【问题描述】:

我正在尝试读取 . XML 文档: /stagingform

我的代码为 $dir 返回一个空值,你能告诉我我做错了什么吗?

$xml = new SimpleXMLElement(file_get_contents("configfile.xml"));
$dir = $xml->codedir;
echo "* * * * directory * * ** -> ".$dir;

我可以看到 configfile.xml 的内容:

echo htmlentities($xml->asXML());

但我无法将 $dir 设置为 /stagingform。 谢谢

【问题讨论】:

  • 你能把xml文件或者它的相关部分贴出来吗?

标签: php parsing xml-parsing simplexml


【解决方案1】:

应该这样做:

$xml = new SimpleXMLElement(file_get_contents("configfile.xml"));
foreach($xml->children() as $child)
{
    if($child->getName()=="codedir"){
        echo $child;
    }

}

【讨论】:

    【解决方案2】:

    一个更容易使用的函数是simplexml_load_file,像这样使用(支持远程/本地文件):

    <?php
    $xml = simplexml_load_file("http://www.breakingnews.com/feeds/rss/");
    
    print_r($xml);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多