SimpleXMLElement (PHP) 在 XML 根属性上引发错误

发布于 2024-09-01 17:07:30 字数 3887 浏览 0 评论 0原文

我正在开发一个项目,该项目将获取通过表单中的文本区域输入提交的 XML 数据,然后提取数据并将其放入数据库中。我收到有关 XML 根字段中的属性的错误(每次都是相同的架构,但具有不同的值)。如果我删除这些属性,它可以正常工作,但我不想每次将数据发送到脚本时都必须删除这些属性。

这是我正在使用的数据和代码的示例(给我错误的部分):

<raid generatedFrom="HeadCount" version="1.7.4">
--snip--
</raid>

如果我这样发布数据,我会收到如下错误:

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

如果我从根节点中删除属性,那么它可以正常工作没有错误。

这是我用来获取和显示它的代码;

<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>

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.

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2024-09-08 17:07:30

我怀疑您启用了 magic_quote,因此反斜杠被添加到 XML 中。

尝试删除反斜杠,

$xml_d = stripslashes($_POST['h_input']);

I suspect you have magic_quote on so the back-slashes are added to the XML.

Try to remove backslashes,

$xml_d = stripslashes($_POST['h_input']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文