Jump to content

XML Parsing Error


whereschp

Recommended Posts

This is the code.

<?php
header('Content-Type: text/xml;charset=UTF-8');

//see Listing 3-9 for icon related items in the code

$lat = (float)$_GET['lat'];
$lng = (float)$_GET['lng'];
$name = htmlspecialchars(strip_tags(utf8_encode($_GET['name'])));
$address = htmlspecialchars(strip_tags(utf8_encode($_GET['address'])));
$type = htmlspecialchars(strip_tags(utf8_encode($_GET['type'])));

//Create an XML node
$marker = <<<MARKER
<marker lat="$lat" lng="$lng" name="$name" address="$address" type="$type"/>
MARKER;

//open the data.xml file for appending
$f=@fopen('data.xml', 'a+');
if(!$f) die('<?xml version="1.0"?>
<response type="error"><![CDATA[Could not open data.xml file]]></response>
');

//add the node
$w=@fwrite($f, $marker);
if(!$w) die('<?xml version="1.0"?>
<response type="error"><![CDATA[Could not write to data.xml file]]></response>');

@fclose($f);

?>

 

And after more than one line is entered in the xml file is returns this.

 

XML Parsing Error: junk after document element

Location: http://whereschp.com/test/data.xml

Line Number 1, Column 53:

 

Any help is appreciated.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/205883-xml-parsing-error/
Share on other sites

It's not a valid xml document.  Xml documents usually start with a XML declaration (<?xml version="1.0" encoding="UTF-8" ?>) followed by a root element which contains all your other elements.  You need something like this:

<?xml version="1.0" encoding="UTF-8" ?>
<markers>
    <marker .... />
    <marker .... />
</markers>

 

If you're appending to an existing xml document, you'll probably need to parse it with one of the xml libraries and then insert your node.

Link to comment
https://forums.phpfreaks.com/topic/205883-xml-parsing-error/#findComment-1077352
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.