【发布时间】: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 文件已损坏。查看
<thumb>http://ecample.com/bla.jpg</thumb>;行 - 看到那个分号了吗? -
@michail_w 我说错了,但这不是问题所在。我已经仔细检查了。