gpt4 book ai didi

php - DOMDocument::loadXML(): StartTag: 实体中的元素名称无效

转载 作者:行者123 更新时间:2023-12-02 04:13:10 26 4
gpt4 key购买 nike

这是我的 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
)

)

)

)

)

最佳答案

正如评论中提到的,问题在于您的数字元素, <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();

通过这种方法,您仍然可以轻松地检索错误。

例如

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>

关于php - DOMDocument::loadXML(): StartTag: 实体中的元素名称无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35365224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com