XML parserror: junk after document element

2019-09-21 02:24发布

问题:

i keep getting a xml parse error, and i think its gotta do with spaces and comments, but since i'm kinda new to php, mysql and xml i'm not 100% sure if the following code is correct.

I am getting the error:

XML-parsefout: troep na documentelement
Locatie: xml.php
Regelnummer 10, kolom 1:<project id="90">
^

Translated:

XML-parseerror: junk after document element
Location: xml.php
Regelnummer 10, kolom 1:<project id="90">
^

Code:

<?php
        $sql = "SELECT * FROM projecten ORDER BY id";
        $res = mysql_query($sql);

        $version = '1.0';
        $encoding = 'UTF-8';

        $xml = new XMLWriter();
        $xml->openURI("php://output");
        $xml->startDocument();
        $xml->setIndent(true);

        //WHILE LOOP OM ALLE GEGEVENS UIT DE QUERY TE HALEN
        while ($row = mysql_fetch_assoc($res)) {

            //START PROJECT
            $xml->startElement('project');
            $xml->writeAttribute('id', $row['id']);

                //ID
                $xml->startElement("id");
                $xml->writeRaw($row['id']);
                $xml->endElement();

                //PROJECTNAAM
                $xml->startElement("projectnaam");
                $xml->writeRaw($row['projectnaam']);
                $xml->endElement();

                //PROJECTSTATUS
                $xml->startElement("status");
                $xml->writeRaw($row['status']);
                $xml->endElement();

                //KLANTNAAM
                $xml->startElement("klantnaam");
                $xml->writeRaw($row['klantnaam']);
                $xml->endElement();

                //STARTDATUM
                $xml->startElement("startdatum");
                $xml->writeRaw($row['startdatum']);
                $xml->endElement();

                //EINDDATUM
                $xml->startElement("eindddatum");
                $xml->writeRaw($row['einddatum']);
                $xml->endElement();

            //EINDE PROJECT
            $xml->endElement();
        }

        //EINDE DOCUMENT
        $xml->endDocument(); 

        header('Content-type: text/xml');
        $xml->flush();
    ?>

This is what the browser generates:

<parsererror>
XML-parsefout: troep na documentelement Locatie: xml.php Regelnummer 10, kolom 1:
<sourcetext><project id="90"> ^</sourcetext>
</parsererror>

回答1:

    /**
     *  Rewrite MySQL to MySQLi
     *  http://php.net/manual/en/migration55.deprecated.php
    **/
    $sql = "SELECT * FROM projecten ORDER BY id";
    $res = mysqli_query($sql);

    /**
     *  Adjusted startDoc to include
     *  Encoding and set Indent to tab
     *  instead of spacing.
    **/
    $xml = new XMLWriter();
    $xml->openURI("php://output");
    $Xml->setIndentString ( "&#09;" );
    $Xml->startDocument( '1.0', 'UTF-8', 'no' );
    $xml->setIndent(true);

    /**
     *  Re-usable function to
     *  write a Start/End Element 
     *  to XMLWriter.
     *
     *  Uses CDdata for longer text strings
    **/
    function addNode( $xml, $NodeName, $Value ) {
        $xml->startElement( $NodeName );
        $xml->writeCData( $Value );
        $xml->endElement();      
    }

    /**
     *  Start a Root Element
    **/
   $xml->startElement('projects');

    /**
     *  LoopThrough each Database row.
    **/
   while ($row = mysqli_fetch_assoc($res)) {

        //START PROJECT
        $xml->startElement('project');
        $xml->writeAttribute('id', $row['id']);

            addNode( $xml, "Id", $row["id"] );
            addNode( $xml, "projectnaam", $row['projectnaam'] );
            addNode( $xml, "status", $row['status'] );
            addNode( $xml, "klantnaam", $row['klantnaam'] );
            addNode( $xml, "startdatum", $row['startdatum'] );
            addNode( $xml, "eindddatum", $row['eindddatum'] );

        //End 'Project'.
        $xml->endElement();
    }

    //End Root Element.
    $xml->endElement();

    //EINDE DOCUMENT
    $xml->endDocument(); 

    header('Content-type: text/xml');
    $xml->flush();
  • Changed to MySQL to MySQLi! It's important to start changing now while you can! =)
  • Use Functions! You write $xml->startElement & $xml->endElement repeatedly, placed it into an addNode() method.
  • Added a 'Root' node to append each database row too.
  • Use CData (<![CDATA[text for inclusion within CData tags]]>), it'll help to safetly escape any difficult possible characters


回答2:

XML must only have one single root. So add a root element which contains all the other elements.

For example:

<?xml version="1.0"?>
<projects>
 <project id="89">
  <id>89</id>
  <projectnaam>XML Feed vacatures amikappers.nl</projectnaam>
  <status>false</status>
  <klantnaam>AMI kappers</klantnaam>
  <startdatum>2013-10-14</startdatum>
  <eindddatum>2013-10-25</eindddatum>
 </project>
 <project id="90">
  <id>90</id>
  <projectnaam>Wifi startpagina AMI Kappers</projectnaam>
  <status>false</status>
  <klantnaam>AMI kappers</klantnaam>
  <startdatum>2013-10-14</startdatum>
  <eindddatum>2013-11-15</eindddatum>
 </project>
</projects>