SimpleXMLElement (PHP) throwing error on XML root attributes

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-05-17T20:38:41Z Indexed on 2010/05/17 20:40 UTC
Read the original article Hit count: 349

Filed under:
|
|
|

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>

Any suggestions would be most helpful, thank you.

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml