SimpleXMLElement (PHP) 在 XML 根属性上抛出错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2852601/
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
SimpleXMLElement (PHP) throwing error on XML root attributes
提问by Greg
I am working on a project that will take XML data submitted through a textarea input in a form then extract the data and throw it into a database. I am getting errors about the attribute in the root field of my XML (it is the same schema each time, but with different values). If i remove these attributes it works fine, but I don't want to have to remove these attributes each I time I go to send the data to the script.
我正在开发一个项目,该项目将通过表单中的 textarea 输入提交 XML 数据,然后提取数据并将其放入数据库中。我在 XML 的根字段中收到有关属性的错误(每次都是相同的架构,但具有不同的值)。如果我删除这些属性,它工作正常,但我不想每次去将数据发送到脚本时都必须删除这些属性。
here is a sample of my data and code I am using (the part that is giving me errors):
这是我正在使用的数据和代码示例(给我错误的部分):
<raid generatedFrom="HeadCount" version="1.7.4">
--snip--
</raid>
If I post the data as such, i get errors such as:
如果我这样发布数据,我会收到如下错误:
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 1: parser error : AttValue: " or ' expected in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: <raid generatedFrom=\"HeadCount\" version=\"1.7.4\"> in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 1: parser error : attributes construct error in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: <raid generatedFrom=\"HeadCount\" version=\"1.7.4\"> in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 1: parser error : Couldn't find end of Start Tag raid line 1 in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: <raid generatedFrom=\"HeadCount\" version=\"1.7.4\"> in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 1: parser error : Extra content at the end of the document in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: <raid generatedFrom=\"HeadCount\" version=\"1.7.4\"> in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php:13 Stack trace: #0 /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php(13): SimpleXMLElement->__construct('<raid generated...') #1 {main} thrown in /home/content/g/V/i/gViscardi/html/guilds/sanctum/headcount.php on line 13
If I remove the attributes from the root node then it works fine with no errors.
如果我从根节点中删除属性,那么它可以正常工作,没有错误。
Here is the code I am using to take and display it;
这是我用来获取和显示它的代码;
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['h_input']))
{
//echo
//$xml_d = file_get_contents('php://input');
$xml_d = $_POST['h_input'];
//print_r($xml_d);
$xml = new SimpleXMLElement($xml_d);
echo $xml->zone . "<br />";
foreach ($xml->players->player as $player) {
echo $player->name . "<br />";
}
} else {
?>
<form action="headcount.php" method="post" name="headcount">
<textarea rows="10" cols="30" name="h_input">
</textarea>
<input type="submit" />
</form>
</body>
</html>
回答by ZZ Coder
I suspect you have magic_quote on so the back-slashes are added to the XML.
我怀疑您打开了 magic_quote,因此将反斜杠添加到 XML 中。
Try to remove backslashes,
尝试删除反斜杠,
$xml_d = stripslashes($_POST['h_input']);

