用domDocument在PHP中解析XML
我有一个Xml,它看起来像用domDocument在PHP中解析XML,php,html,xml,Php,Html,Xml,我有一个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="t
<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>';
}
}
我得到的结果是
试验
你好
{{rating}{{content}}这里是一段点击我!这里又是一段!
代码材料
代码材料
世界
代码材料
代码材料
代码材料
您可以在这里看到,大多数html在这里都不起作用。。请帮助首先,您必须了解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\u html进行了更改,但我认为您现在可以完成其余操作。您的XML文件已损坏。看这条线http://ecample.com/bla.jpg; - 看到那个分号了吗?@michail\u我把它放错了,但这不是问题所在。我仔细检查过了,它能用。。非常感谢,很抱歉,我不能给你的答案打分,因为我的名声不太好,因为我是个新手。但我感谢你的帮助: