使用PHP和MYSQL将多个属性填充到XML中

使用PHP和MYSQL将多个属性填充到XML中,php,xml,dom,Php,Xml,Dom,我想使用PHP将mysql表中的记录保存到XML文件中。我正在成功地检索记录并保存到数据库中。 我想用customer表中的所有记录填充xml。(CustomerAccountNumber、CustomerAccountName、AccountBalance、InvAddressLine1) 这是我的密码: <? $newAcct=$db_accnt; $newAcctname=$db_accntname; $newAcctbalance=$db_accntbalance; $xml

我想使用PHP将mysql表中的记录保存到XML文件中。我正在成功地检索记录并保存到数据库中。 我想用customer表中的所有记录填充xml。(CustomerAccountNumber、CustomerAccountName、AccountBalance、InvAddressLine1)

这是我的密码:

<?
  $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>


我不知道为每条记录生成多个属性需要做什么。请帮助我

即使它是XML,它仍然是一个文本文件,您也可以使用
file\u put\u contents()编写它

$xml='1!'
';
而($row=$result->mysqli\u fetch\u array()){
$xml.='';
}
$xml.='';
文件内容(“customer.xml”,$xml);

即使它是XML,它仍然是一个文本文件,您也可以使用
file\u put\u contents()
编写它

$xml='1!'
';
而($row=$result->mysqli\u fetch\u array()){
$xml.='';
}
$xml.='';
文件内容(“customer.xml”,$xml);

< /代码> 您可能需要考虑使用SimeXML,这将帮助您生成XML。它将非常简单:

$row->addAttribute("CustomerAccountName", "name");

这里的文档:

您可能需要考虑使用SimeXML,这将帮助您生成XML。它将非常简单:

$row->addAttribute("CustomerAccountName", "name");
此处的文档:

您可以使用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();
您可以使用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();