这是我的xml:
<?xml version="1.0"?>
<data>
<client_name>Awesome Client</client_name>
<account_number/>
<date_created>02/12/2016</date_created>
<form_number>4126</form_number>
<customer_po/>
<terms_name>Credit Card</terms_name>
<date_shipped>12/31/1969</date_shipped>
<billing_contact_email/>
<billing_contact_address_line_1/>
<billing_contact_address_line_2/>
<billing_contact_address_line_3/>
<billing_contact_address_line_4/>
<billing_contact_address_city/>
<billing_contact_address_state>British Columbia</billing_contact_address_state>
<billing_contact_address_postal/>
<billing_contact_address_country>Canada</billing_contact_address_country>
<shipping_contact_address_line_1/>
<shipping_contact_address_line_2/>
<shipping_contact_address_line_3/>
<shipping_contact_address_line_4/>
<shipping_contact_address_city/>
<shipping_contact_address_state>British Columbia</shipping_contact_address_state>
<shipping_contact_address_postal/>
<shipping_contact_address_country>Canada</shipping_contact_address_country>
<billing_contact_first_name>another</billing_contact_first_name>
<billing_contact_last_name>client</billing_contact_last_name>
<client_rep_full_name>Rob Montebelli</client_rep_full_name>
<order_rep_full_name>Mark Graham</order_rep_full_name>
<job_name>77777</job_name>
<job_number>2620</job_number>
<event_type>Donor Gift</event_type>
<due_date>02/12/2016</due_date>
<shipping_method/>
<currency>CAD</currency>
<total_taxes>0.00</total_taxes>
<total_subtotal>1,760.16</total_subtotal>
<total>1,760.16</total>
<items>
<item0>
<taxes>
<0>E</0>
</taxes>
<title>1889-24</title>
<quantity>6</quantity>
<description>Carhartt (R) Signature Utility Duffel; TBD TBD</description>
<unit_price>159.32</unit_price>
</item0>
<item1>
<taxes>
<0>E</0>
</taxes>
<title>0022-56</title>
<quantity>12</quantity>
<description>Zoom (TM) DayTripper Sling Compu-Messenger; TBD TBD</description>
<unit_price>67.02</unit_price>
</item1>
</items>
</data>
我的代码:
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
array_to_xml($invoice, $xml_data);
$xml = $xml_data->asXML();
$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
$domxml->loadXML($xml);
$xml_string = $domxml->saveXML();
我认为xml格式正确,为什么会失败呢?
编辑:
这是使用XMLSimpleElement转换为xml之前的动态数组,无论如何都要删除&lt; 0&gt;在转换成xml之前?:
Array
(
[invoices] => Array
(
[0] => Array
(
[client_name] => Awesome Client
[account_number] =>
[date_created] => 02/11/2016
[form_number] => 4104
[customer_po] =>
[terms_name] => Credit Card
[date_shipped] => 12/31/1969
[billing_contact_email] =>
[billing_contact_address_line_1] =>
[billing_contact_address_line_2] =>
[billing_contact_address_line_3] =>
[billing_contact_address_line_4] =>
[billing_contact_address_city] =>
[billing_contact_address_state] => British Columbia
[billing_contact_address_postal] =>
[billing_contact_address_country] => Canada
[shipping_contact_address_line_1] =>
[shipping_contact_address_line_2] =>
[shipping_contact_address_line_3] =>
[shipping_contact_address_line_4] =>
[shipping_contact_address_city] =>
[shipping_contact_address_state] => British Columbia
[shipping_contact_address_postal] =>
[shipping_contact_address_country] => Canada
[billing_contact_first_name] => another
[billing_contact_last_name] => client
[client_rep_full_name] => Rob Montebelli
[order_rep_full_name] => Mark Graham
[job_name] => 5010
[job_number] => 2598
[event_type] => Donor Gift
[due_date] => 02/11/2016
[shipping_method] =>
[currency] => CAD
[total_taxes] => 0.00
[total_subtotal] => 1,760.16
[total] => 1,760.16
[items] => Array
(
[0] => Array
(
[taxes] => Array
(
[0] => E
)
[title] => 1889-24
[quantity] => 6
[description] => Carhartt (R) Signature Utility Duffel; TBD TBD
[unit_price] => 159.32
)
[1] => Array
(
[taxes] => Array
(
[0] => E
)
[title] => 0022-56
[quantity] => 12
[description] => Zoom (TM) DayTripper Sling Compu-Messenger; TBD TBD
[unit_price] => 67.02
)
)
)
)
)
答案 0 :(得分:2)
元素名称必须以字母或下划线开头
观看http://www.w3schools.com/xml/xml_elements.asp了解更多信息
更改
<taxes>
<0> E </0>
</taxes>
到
<taxes>
<t0> E </t0>
</taxes>
例如
答案 1 :(得分:2)
正如评论中所提到的,问题在于您的数字元素<0>
。这是一个无效的元素名称。如果这是一个静态文件,删除或重命名,使用正则表达式或抑制错误,您可以手动修改XML文件。如果你压制,你将不得不记住,将来它永远不会告诉你错误。
正则表达式方法:
$invoice = preg_replace('~<(/?\d)~', '<number$1', $invoice);
正则表达式演示:https://regex101.com/r/pZ3sJ4/1
一个抑制选项:
libxml_use_internal_errors(true);
$domxml->loadXML($invoice);
libxml_clear_errors();
使用这种方法,您仍然可以非常轻松地检索错误。
e.g。
libxml_use_internal_errors(true);
$domxml->loadXML($invoice);
$xml_string = $domxml->saveXML();
$errors = libxml_get_errors();
foreach ($errors as $error) {
echo $error->message;
}
libxml_clear_errors();
第二个抑制选项:
@$domxml->loadXML($invoice);
呸..
第三个简单的非编程选项,只需手动编辑文件:
<item1>
<taxes><name0>E</name0></taxes>
<title>0022-56</title>
<quantity>12</quantity>
<description>Zoom (TM) DayTripper Sling Compu-Messenger; TBD TBD</description>
<unit_price>67.02</unit_price>
</item1>