我一直得到一个xml解析错误,我认为它必须使用空格和注释,但因为我是一个新的php,mysql和xml我不是100%确定如果以下代码是正确的。< / p>
我收到错误:
XML-parsefout: troep na documentelement
Locatie: xml.php
Regelnummer 10, kolom 1:<project id="90">
^
翻译:
XML-parseerror: junk after document element
Location: xml.php
Regelnummer 10, kolom 1:<project id="90">
^
代码:
<?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();
?>
这是浏览器生成的内容:
<parsererror>
XML-parsefout: troep na documentelement Locatie: xml.php Regelnummer 10, kolom 1:
<sourcetext><project id="90"> ^</sourcetext>
</parsererror>
答案 0 :(得分:2)
/**
* 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 ( "	" );
$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();
$xml->startElement
&amp; $xml->endElement
重复addNode()
,将其置于<![CDATA[text for inclusion within CData tags]]>
方法中。 CData
({{1}}),这有助于安全逃脱任何困难的可能角色答案 1 :(得分:0)
XML必须只有一个根。所以添加一个包含所有其他元素的根元素。
例如:
<?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>