我希望有人可以帮助我。我有一个生成XML文件的脚本:
<Reeleezee version="1.15" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.reeleezee.nl/taxonomy/1.15 taxonomy\1.15\Reeleezee.xsd" xmlns="http://www.reeleezee.nl/taxonomy/1.15">
<Import>
<CustomerList>
<Customer>
<ID/>
<FullName>Dennis Rijken</FullName>
<SearchName>Dennis Rijken</SearchName>
<Code/>
<DefaultAddress>Office</DefaultAddress>
<LanguageCode>nl</LanguageCode>
<FaxNumber/>
<EmailAddress/>
<WebsiteAddress/>
<Comment/>
<ChamberOfCommerceNumber/>
<ChamberOfCommerceCity/>
<FiscalIdentificationNumber/>
<TaxDepositLHNumber/>
<TaxDepositOBNumber/>
<TaxDepositICLNumber/>
<BankAccountNumber/>
<BranchReference/>
<AddressList>
<Address Type="Office">
<Street>Stuivenes</Street>
<Number>11</Number>
<Zipcode>3911 XX</Zipcode>
<City>Rhenen</City>
<CountryCode>NL</CountryCode>
</Address>
</AddressList>
<ContactPersonList>
<ContactPersonReference/>
</ContactPersonList>
</Customer>
</CustomerList>
</Import>
但它应该是这样的:
<Customer RlzID="23232.51.8C8D54156584290">
<ID xsi:nil="true" />
<FullName>GPXS</FullName>
<SearchName>GPXS</SearchName>
<Code xsi:nil="true" />
<DefaultAddress>Office</DefaultAddress>
<LanguageCode>nl</LanguageCode>
<PhoneNumber xsi:nil="true" />
<FaxNumber xsi:nil="true" />
<EmailAddress xsi:nil="true" />
<WebsiteAddress xsi:nil="true" />
<Comment xsi:nil="true" />
<ChamberOfCommerceNumber xsi:nil="true" />
<ChamberOfCommerceCity xsi:nil="true" />
<FiscalIdentificationNumber xsi:nil="true" />
<TaxDepositLHNumber xsi:nil="true" />
<TaxDepositOBNumber xsi:nil="true" />
<TaxDepositICLNumber xsi:nil="true" />
<BankAccountNumber xsi:nil="true" />
<BranchReference xsi:nil="true" />
<AddressList>
<Address Type="Office">
<Street>Stephensonstraat</Street>
<Number>19</Number>
<Zipcode>1097 BA</Zipcode>
<City>Amsterdam</City>
<CountryCode>NL</CountryCode>
</Address>
<Address Type="Delivery" xsi:nil="true" />
</AddressList>
<ContactPersonList>
<ContactPersonReference RlzID="23232.89.8CA58ACEACD0560" />
</ContactPersonList>
</Customer>
PHP脚本生成的xml如下:
$query = "SELECT orders.id AS orderid, DATE_FORMAT(orders.datum, '%d-%m-%Y %k:%i:%s') AS datum, orders.bedrag, debiteur.naam, debiteur.achternaam, debiteur.adres, debiteur.huisnummer, debiteur.toevoeging, debiteur.postcode, debiteur.plaats, debiteur.rek_nummer, debiteur.email
FROM orders
LEFT JOIN debiteur ON orders.debiteur_id = debiteur.id";
$result = mysql_query($query) or die(mysql_error());
$exportXML = new SimpleXMLElement("<Reeleezee></Reeleezee>");
$import = $exportXML->addChild('Import');
$customerlist = $import->addChild('CustomerList');
while($res = mysql_fetch_array($result, MYSQL_ASSOC)){
$customer = $customerlist->addChild('Customer');
$customer->addChild('ID');
$customer->addChild('FullName', $res['naam']." ".$res['achternaam']);
$customer->addChild('SearchName', $res['naam']." ".$res['achternaam']);
$customer->addChild('Code');
$customer->addChild('DefaultAddress', 'Office');
$customer->addChild('LanguageCode', 'nl');
$customer->addChild('FaxNumber');
$customer->addChild('EmailAddress');
$customer->addChild('WebsiteAddress');
$customer->addChild('Comment');
$customer->addChild('ChamberOfCommerceNumber');
$customer->addChild('ChamberOfCommerceCity');
$customer->addChild('FiscalIdentificationNumber');
$customer->addChild('TaxDepositLHNumber');
$customer->addChild('TaxDepositOBNumber');
$customer->addChild('TaxDepositICLNumber');
$customer->addChild('BankAccountNumber');
$customer->addChild('BranchReference');
$addresslist = $customer->addChild('AddressList');
$address = $addresslist->addChild('Address');
$address->addAttribute('Type', 'Office');
$address->addChild('Street', $res['adres']);
$address->addChild('Number', $res['huisnummer']);
$address->addChild('Zipcode', $res['postcode']);
$address->addChild('City', $res['plaats']);
$address->addChild('CountryCode', 'NL');
$contactpersonlist = $customer->addChild('ContactPersonList');
$contactpersonlist->addChild('ContactPersonReference');
}
Header('Content-type: text/xml');
$strxml = $exportXML->asXML();
$root = '<Reeleezee version='.'"1.15"'.' xmlns:xsi='.'"http://www.w3.org/2001/XMLSchema-instance"'.' xsi:schemaLocation='.'"http://www.reeleezee.nl/taxonomy/1.15 taxonomy\1.15\Reeleezee.xsd"'.' xmlns='.'"http://www.reeleezee.nl/taxonomy/1.15"'.'>';
$newxml = str_replace("<Reeleezee>", $root, $strxml);
//echo $newxml;
file_put_contents("test.xml", $newxml);
如何将 xsi:nil =“true”添加到XML元素?
有人可以帮帮我吗?如何定义XML元素类型,如整数,字符串等。
答案 0 :(得分:1)
如果需要命名空间,则必须编写命名空间的前缀+属性的名称(&#34;前缀:name&#34;)作为第一个参数,将uri作为第三个参数。
示例代码:
<?php
$xml = new SimpleXMLElement("<packagedElement></packagedElement>");
$xml->addAttribute("xsi:nil", "true", "http://www.w3.org/2001/XMLSchema-instance");
echo $xml->asXml();
?>
输出:
<packagedElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"></packagedElement>
修改强>
在你的情况下:(试试这个)
更改
$customer->addChild('ID');
到
$customer->addChild('ID')->addAttribute("xsi:nil", "true");