SimpleXMLElement(PHP)在XML根属性上抛出错误

时间:2010-05-17 20:38:41

标签: xml post simplexml php

我正在开发一个项目,该项目将通过表单中的textarea输入提交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>

1 个答案:

答案 0 :(得分:6)

我怀疑你有magic_quote,所以反斜杠被添加到XML中。

尝试删除反斜杠,

$xml_d = stripslashes($_POST['h_input']);
相关问题