DOMDocument::loadXML(): StartTag: 实体中无效的元素名称
DOMDocument::loadXML(): StartTag: invalid element name in Entity
这是我的 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 之前的动态数组,是否可以在转换为 xml 之前删除 <0>?:
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
)
)
)
)
)
Element names must start with a letter or underscore
观看http://www.w3schools.com/xml/xml_elements.asp了解更多信息
改变
<taxes>
<0> E </0>
</taxes>
到
<taxes>
<t0> E </t0>
</taxes>
例如
如评论中所述,问题出在您的数字元素 <0>
上。这是一个无效的元素名称。如果这是静态文件,您可以手动修改 XML 文件、删除或重命名、使用正则表达式或抑制错误。如果你打压了以后你要记住,它永远不会告诉你错误的。
正则表达式方法:
$invoice = preg_replace('~<(/?\d)~', '<number', $invoice);
正则表达式演示:https://regex101.com/r/pZ3sJ4/1
一个抑制选项:
libxml_use_internal_errors(true);
$domxml->loadXML($invoice);
libxml_clear_errors();
使用这种方法,您仍然可以很容易地检索错误。
例如
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>
这是我的 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 之前的动态数组,是否可以在转换为 xml 之前删除 <0>?:
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
)
)
)
)
)
Element names must start with a letter or underscore
观看http://www.w3schools.com/xml/xml_elements.asp了解更多信息
改变
<taxes>
<0> E </0>
</taxes>
到
<taxes>
<t0> E </t0>
</taxes>
例如
如评论中所述,问题出在您的数字元素 <0>
上。这是一个无效的元素名称。如果这是静态文件,您可以手动修改 XML 文件、删除或重命名、使用正则表达式或抑制错误。如果你打压了以后你要记住,它永远不会告诉你错误的。
正则表达式方法:
$invoice = preg_replace('~<(/?\d)~', '<number', $invoice);
正则表达式演示:https://regex101.com/r/pZ3sJ4/1
一个抑制选项:
libxml_use_internal_errors(true);
$domxml->loadXML($invoice);
libxml_clear_errors();
使用这种方法,您仍然可以很容易地检索错误。
例如
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>