PHP 错误:DOMDocument::loadXML() [domdocument.loadxml]:需要开始标记,在实体中找不到“<”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8270141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Error: DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity
提问by Hearaman
Please check this bellow program.
请检查这个波纹管程序。
::Program::
::程序::
<?php
$xml='
<books>
<book>
<name>Java complete reference</name>
<cost>256</cost>
</book>
<book>
<name>Head First PHP and Mysql</name>
<cost>389</cost>
</book>
</books>';
$dom=new DOMDocument();
$dom->loadXML($xml);
foreach ($dom->getElementsByTagName('book') as $book)
{
foreach($book->getElementsByTagName('name') as $name)
{
$names[]=$name->nodeValue;
}
foreach($book->getElementsByTagName('cost') as $cost)
{
$costs[]=$cost->nodeValue;
}
}
print_r($names);
?>
It is shows error:
它显示错误:
DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity
DOMDocument::loadXML() [domdocument.loadxml]:需要开始标记,在实体中找不到“<”
Is this correct way to do this?
这是正确的方法吗?
If it is correct, Is there any way to get the proper result without changing this <
to <
and >
to >
?
如果它是正确的,有没有什么办法让没有这种改变正确的结果<
,以<
及>
对>
?
回答by Trott
You should not be using character entities for <
and >
on things that are actually XML tags in the string that represents your XML. It should be this:
你不应该使用字符实体<
和>
对事情实际上是代表你的XML字符串中的XML标记。应该是这样的:
$xml='
<books>
<book>
...
Do that and the warning goes away.
这样做,警告就会消失。
You only need to use character entities for <
and >
when they are part of the actual data rather than delimiting an XML tag.
你只需要使用字符实体<
和>
当他们的实际数据的一部分,而不是划定一个XML标签。
回答by PeeHaa
DOMDocument expects the string to be VALID xml.
DOMDocument 期望字符串为 VALID xml。
Your string isn't a valid XML string. You should just use <
in stead of <
您的字符串不是有效的 XML 字符串。你应该只用<
代替<
Why would you have the htmlentities in that string?
为什么要在该字符串中包含 htmlentities?
回答by Hearaman
Thank you all. Just now i have tried with the method "html_entity_decode()". It is worked for me for this example.
谢谢你们。刚才我尝试了“html_entity_decode()”方法。对于这个例子,它对我有用。
::Code::
::代码::
<?php
$xml='
<books>
<book>
<name>Java complete reference</name>
<cost>256</cost>
</book>
<book>
<name>Head First PHP and Mysql</name>
<cost>389</cost>
</book>
</books>';
$xml=html_entity_decode($xml);
$dom=new DOMDocument();
$dom->loadXML($xml);
foreach ($dom->getElementsByTagName('book') as $book)
{
foreach($book->getElementsByTagName('name') as $name)
{
$names[]=$name->nodeValue;
}
foreach($book->getElementsByTagName('cost') as $cost)
{
$costs[]=$cost->nodeValue;
}
}
print_r($names);
?>
回答by Fredy31
Aren't you supposed to start with something like this
你不应该从这样的事情开始吗?
<?xml version="1.0" encoding="UTF-8" ?>
to create valid XML? That might be the missing start tag your error is talking about.
创建有效的 XML?这可能是您的错误所谈论的缺少开始标签。