2012-11-13 27 views
0

我想將使用PHP的mysql表中的記錄保存到XML文件中。我正在成功檢索記錄並保存到數據庫中。 。 我想填充我的XML與我的客戶表中的所有記錄(CustomerAccountNumber,CustomerAccountName,AccountBalance,InvAddressLine1)使用PHP和MYSQL將多個屬性填充到XML中

這裏是我的代碼:

<? 
    $newAcct=$db_accnt; 
$newAcctname=$db_accntname; 
$newAcctbalance=$db_accntbalance; 

$xmldoc=new DOMDocument(); 
$xmldoc->load('XML/customer.xml'); 
$newElement = $xmldoc->createElement('Row'); 
$newAttribute = $xmldoc->createAttribute('CustomerAccountNumber'); 
$newAttribute->value = $newAct; 
$newElement->appendChild($newAttribute); 
$root->appendChild($newElement); 
?> 

我的問題是:

如何生成customer.xml以及如何以有效的方式保存數千條記錄,以便基於中的數據庫此格式

<?xml version="1.0" standalone="yes"?> 
    <Rows> 
    <Row CustomerAccountNumber="CU002" CustomerAccountName="Customer 1" AccountBalance="289.00" /> 
    <Row CustomerAccountNumber="5U002" CustomerAccountName="Customer 2" AccountBalance="1899.00" /> 
    <Row CustomerAccountNumber="CU004" CustomerAccountName="Customer 3" AccountBalance="289.00" /> 
    <Row CustomerAccountNumber="5U032" CustomerAccountName="Customer 4" AccountBalance="1899.00" /> 
    </Rows> 

我沒有得到什麼線索,我需要做的產生多個屬性爲每個record.Kindly幫我

回答

2

您可以使用XMLWriter的

$xml =new XMLWriter(); 
$xml->openURI('file.xml'); 
$xml->setIndent(true); 
$xml->startDocument('1.0', 'UTF-8', 'yes'); 
$xml->startElement('Rows'); 
while (// fetch from DB) { 
    $xml->startElement('Row'); 
    $xml->writeattribute("CustomerAccountNumber", "1"); 
    $xml->writeattribute("CustomerAccountName", "2"); 
    $xml->writeattribute("AccountBalance", "3"); 
    $xml->endElement(); 
} 
$xml->endElement(); 
$xml->flush(); 
2

即使它是一個XML,它仍然是一個文本文件,你可以只用file_put_contents()寫。

$xml='<?xml version="1.0" standalone="yes"?> 
<Rows>'; 

while($row=$result->mysqli_fetch_array()){ 
    $xml.='<Row CustomerAccountNumber="'.$row[0].'" CustomerAccountName="'.$row[1].'" AccountBalance="'.$row[2].'" />'; 
} 
$xml.='</Rows>'; 
file_put_contents("customer.xml", $xml);