【问题标题】:XML Parsing in PHP with domDocument使用 domDocument 在 PHP 中解析 XML
【发布时间】:2014-08-12 07:43:51
【问题描述】:

我有一个看起来像

的 Xml
<theme>
<name>Test</name>
<thumb>http://ecample.com/bla.jpg</thumb>;
<template>
<name>Hello</name>
<html>
<body> 
<div id="hell">
<input type="text" name="text1" id="text1" value="Type Some thing"/>
<input type="button" name="button1" id="button1" value="Button" />

<div class="hello">
<p>here is a paragraph</p>
</div>
<div class="hello123">
    <p><a href="#">Click Me!</a>here is a paragraph again!</p>
</div>
<textarea name="hello"></textarea>
</div>
</body> 
</html>
<css> CODE STUFF </css>
<javascript> CODE STUFF </javascript>
</template>
<template>
<name>World!</name>
<html> CODE STUFF </html>
<css> CODE STUFF </css>
<javascript> CODE STUFF </javascript>
</template>
</theme>

我想获取所有的 html 标签,因为它们在 body 标签中。但是当我使用 domDocument 获取 html 标签时,大多数标签都丢失了。这是我下面的代码

$doc = new DOMDocument();
    $doc->loadXML( $xml_file_string );//xml file loading here
    $themes = $doc->getElementsByTagName( "theme" );
    foreach( $themes as $theme )
    {
        $theme_name = $theme->getElementsByTagName( "name" );
        $theme_thumb = $theme->getElementsByTagName( "thumb" );
        $theme_name = $theme_name->item(0)->nodeValue;
        $theme_thumb = $theme_thumb->item(0)->nodeValue;
        echo $theme_name.'<br>';
        echo $theme_thumb.'<br>';
        $templates = $theme->getElementsByTagName( "template" );
        foreach( $templates as $template )
        {
            $template_name = $template->getElementsByTagName( "name" );
            $template_name = $template_name->item(0)->nodeValue;
            $template_html = $template->getElementsByTagName( "html" );
            $template_html = $template_html->item(0)->nodeValue;
            $template_css  = $template->getElementsByTagName( "css" );
            $template_css  = $template_css->item(0)->nodeValue;
            $template_javascript = $template->getElementsByTagName( "javascript" );
            $template_javascript = $template_javascript->item(0)->nodeValue;
            echo $template_name.'<br>';
            echo html_entity_decode($template_html).'<br>';
            echo $template_css.'<br>';
            echo $template_javascript.'<br>';
        }
    }

我得到的结果是,

测试 http://ecample.com/bla.jpg 你好 {{rating}} {{content}}这是一段点击我!这里又是一段! 代码资料 代码资料 世界! 代码资料 代码资料 代码东西

您可以在这里看到大部分 html 在这里都不起作用..请帮助

【问题讨论】:

  • 您的 XML 文件已损坏。查看&lt;thumb&gt;http://ecample.com/bla.jpg&lt;/thumb&gt;; 行 - 看到那个分号了吗?
  • @michail_w 我说错了,但这不是问题所在。我已经仔细检查了。

标签: php html xml


【解决方案1】:

首先,您必须了解,getElementsByTagName 方法和任何其他 getter 返回类 DOMNode 的对象(或对象数组)。如果它有内容,但没有包含在任何标签中,则该内容可以由nodeValue 属性返回。你用它来获取模板名称。但 nodeValue 不包含儿童的 html。你必须创造它。这是一个例子:

$tmp_dom = new DOMDocument(); 
$tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
$html = trim($tmp_dom->saveHTML());

所以你的代码应该是这样的:

$doc = new DOMDocument();
$doc->loadXML( $xml_file_string );//xml file loading here
$themes = $doc->getElementsByTagName( "theme" );
foreach( $themes as $theme )
{
    $theme_name = $theme->getElementsByTagName( "name" );
    $theme_thumb = $theme->getElementsByTagName( "thumb" );
    $theme_name = $theme_name->item(0)->nodeValue;
    $theme_thumb = $theme_thumb->item(0)->nodeValue;
    echo $theme_name.'<br>';
    echo $theme_thumb.'<br>';
    $templates = $theme->getElementsByTagName( "template" );
    foreach( $templates as $template )
    {
        $template_name = $template->getElementsByTagName( "name" );
        $template_name = $template_name->item(0)->nodeValue;
        $template_html = $template->getElementsByTagName( "html" );

        //HERE IS CHANGE
        $tmpHtml = new DOMDocument();
        $tmpHtml->appendChild($tmpHtml->importNode($template_html->item(0), true)); 
        $template_html = trim($tmpHtml->saveHTML());

        //REST OF CODE
    }
}

我只对$template_html 进行了更改,但我认为您现在可以完成剩下的工作了。

【讨论】:

  • 它有效.. 非常感谢您的回答我无法评价您的回答,因为我的声誉较低,因为我是 stack-overflow 的新手。但我感谢你的帮助:)
猜你喜欢
  • 2011-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 2013-02-08
相关资源
最近更新 更多