我在加载我的XML文档时遇到了问题。 XML文件是PHP DOMDocument没有返回或加载第一个元素
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE gallery [
<!ELEMENT gallery (category*)>
<!ELEMENT category (image*)>
<!ELEMENT image (title,comment,password,owner,rating)>
<!ELEMENT title ANY>
<!ELEMENT comment ANY>
<!ELEMENT password ANY>
<!ELEMENT owner ANY>
<!ELEMENT rating ANY>
<!ATTLIST category name CDATA #REQUIRED>
<!ATTLIST image date CDATA #REQUIRED>
]>
<gallery>
<category name="entertainment">
<image date="monday">
<title>1</title>
<comment>2</comment>
<password>3</password>
<owner>4</owner>
<rating>5</rating>
</image>
<image date="friday">
<title>11</title>
<comment>22</comment>
<password>33</password>
<owner>44</owner>
<rating>55</rating>
</image>
</category>
<category name="education">
</category>
<category name="relax">
</category>
<category name="sporting">
</category>
</gallery>
而我的PHP打印此XML
$doc = new DOMDocument();
$doc->validateOnParse = true;
$doc->load(ROOT_PATH."xml/data.xml");
if($doc->validate()) {
print_r($doc->saveHTML());
}
else echo 'Not Validated ';
,其结果是
2 3 4 5 22 33 44 55
顺便说一句,我不明白为什么第一个元素是跳过。我有问题,这样的
$category = $doc->getElementById('entertainment');
if (!$category){
echo 'not found';
}
返回选择元素:
not found
你是在浏览器中做这个吗?查看浏览器的_page source_查看XML结构。如果您刚刚看到'2 3 5 22 33 44 55',可能是因为浏览器正在将XML解释为HTML标记。你有什么工作将XML转储回屏幕:http://ideone.com/WtzeKM –
而'娱乐'没有找到,因为没有'ID ='娱乐'元素,尽管你有'名称= entertainment''。 –
感谢提示。但我的浏览器正确地在html中打印这个XML。女巫是1 2 3 4 5 11 22 33 44 55.而浏览器中的xml原始文件也是正确的。 – ianion