2016-04-27 59 views
0

我將數據從我的數據庫轉換爲xml文件的每一行。儘管如此,我仍然不斷收到錯誤信息,但我會告訴你是否有人能夠檢查此代碼並幫助我解決問題。謝謝。php,sql row到xml文件的轉換。需要幫助

PHP:

<?php 
include_once'dbconnect.php'; 
$sql="select * from Applicants WHERE Outcome='A'"; 
$result =mysql_query($sql); 

while($row = mysql_fetch_assoc($result)){ 

print_r($row); 
$count=count($row); 
foreach ($row as $row) 
{ 

     $xml = "<CTfile>". PHP_EOL; 
     $xml .="<DocumentName>Common Transfer File</DocumentName>". PHP_EOL; 
     $xml .="<CTFversion>11.0</CTFversion>". PHP_EOL; 
     $xml .="<DateTime>2011-09-27T00:00:00</DateTime>". PHP_EOL; 
     $xml .="<CTFpupilData>". PHP_EOL; 
     $xml .="<Pupil>". PHP_EOL; 
     $xml .= '<fname>'.'$row["Firstname"]'.'</fname>'. PHP_EOL; 
     $xml .= '<lname>'.'$row["Surname"]'.'</lname>'. PHP_EOL; 
     //$xml .= "</Phones>". PHP_EOL; 
     //$xml .= "<Email>".$row['ApplicantEmail']."</Email>". PHP_EOL; 
     //$xml .= "</Pupil>". PHP_EOL; 
     //$xml .= "</CTFpupilData>". PHP_EOL; 
     //$xml .= "</CTfile>". PHP_EOL; 
     $sxe = new SimpleXMLElement($xml); 
     $sxe->asXML("test.xml"); 


} 

} 
?> 

錯誤:

Warning: SimpleXMLElement::__construct(): Entity: line 9: parser error : Premature end of data in tag Pupil line 6 in C:\xampp\htdocs\A\Convert.php on line 26 

Warning: SimpleXMLElement::__construct(): in C:\xampp\htdocs\A\Convert.php on line 26 

Warning: SimpleXMLElement::__construct():^in C:\xampp\htdocs\A\Convert.php on line 26 

Warning: SimpleXMLElement::__construct(): Entity: line 9: parser error : Premature end of data in tag CTFpupilData line 5 in C:\xampp\htdocs\A\Convert.php on line 26 

Warning: SimpleXMLElement::__construct(): in C:\xampp\htdocs\A\Convert.php on line 26 

Warning: SimpleXMLElement::__construct():^in C:\xampp\htdocs\A\Convert.php on line 26 

Warning: SimpleXMLElement::__construct(): Entity: line 9: parser error : Premature end of data in tag CTfile line 1 in C:\xampp\htdocs\A\Convert.php on line 26 

Warning: SimpleXMLElement::__construct(): in C:\xampp\htdocs\A\Convert.php on line 26 

Warning: SimpleXMLElement::__construct():^in C:\xampp\htdocs\A\Convert.php on line 26 

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\A\Convert.php:26 Stack trace: #0 C:\xampp\htdocs\A\Convert.php(26): SimpleXMLElement->__construct('<CTfile>\r\n<Docu...') #1 {main} thrown in C:\xampp\htdocs\A\Convert.php on line 26 

謝謝

回答

0

我這裏測試Ë做出一些改變。問題是foreach和一些標籤沒有關閉。

$i = 0; 
while($row = mysql_fetch_assoc($result)){ 

    $count=count($row); 
    $i++; 

    $xml = "<CTfile>". PHP_EOL; 
    $xml .="<DocumentName>Common Transfer File</DocumentName>". PHP_EOL; 
    $xml .="<CTFversion>11.0</CTFversion>". PHP_EOL; 
    $xml .="<DateTime>2011-09-27T00:00:00</DateTime>". PHP_EOL; 
    $xml .="<CTFpupilData>". PHP_EOL; 
    $xml .="<Pupil>". PHP_EOL; 
    $xml .= '<fname>'.$row["Firstname"].'</fname>'. PHP_EOL; 
    $xml .= '<lname>'.$row["Surname"].'</lname>'. PHP_EOL; 
    //$xml .= "</Phones>". PHP_EOL; 
    //$xml .= "<Email>".$row['ApplicantEmail']."</Email>". PHP_EOL; 
    $xml .= "</Pupil>". PHP_EOL; 
    $xml .= "</CTFpupilData>". PHP_EOL; 
    $xml .= "</CTfile>". PHP_EOL; 
    $sxe = new SimpleXMLElement($xml); 
    //generate file for each row 
    $sxe->asXML("test_".$i.".xml"); 

}

+1

謝謝你的男人,我已經解決了這一問題,但再一次,非常感謝你。 – srhgrsdhfdh