This is my xml:
这是我的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>
My code:
我的代码:
$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();
I think the xml is in the right format so why is this failing?
我认为xml的格式是正确的,为什么会失败呢?
Edit:
编辑:
Here is the dynamic array before converted into xml using XMLSimpleElement, is there anyway to remove <0> before converted into 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
)
)
)
)
)
2 个解决方案
#1
2
As mentioned in the comments the issue is with your number elements, <0>
. That is an invalid element name. You could manually modify the XML file if this is a static file, remove or rename, use a regex, or suppress the error. If you suppress you will have to remember that in the future, it will never tell you about errors.
正如评论中提到的,问题在于您的number元素<0>。这是一个无效的元素名。如果这是一个静态文件,您可以手动修改XML文件、删除或重命名、使用regex或抑制错误。如果你压抑自己,你必须记住,在未来,它永远不会告诉你错误。
Regex approach:
正则表达式的方法:
$invoice = preg_replace('~<(/?\d)~', '<number$1', $invoice);
Regex demo: https://regex101.com/r/pZ3sJ4/1
Regex演示:https://regex101.com/r/pZ3sJ4/1
One suppression option:
一个抑制选项:
libxml_use_internal_errors(true);
$domxml->loadXML($invoice);
libxml_clear_errors();
With this approach you can still retrieve the errors pretty easily.
使用这种方法,您仍然可以很容易地检索错误。
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();
Second suppression option:
第二个抑制选项:
@$domxml->loadXML($invoice);
yuck..
恶心. .
Third simple non-programming option, just edit the file manually:
第三个简单的非编程选项,只需手动编辑文件:
<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>
#2
2
Element names must start with a letter or underscore
元素名称必须以字母或下划线开头。
Watch http://www.w3schools.com/xml/xml_elements.asp for more information
更多信息请访问http://www.w3schools.com/xml/xml_elements.asp
change
改变
<taxes>
<0> E </0>
</taxes>
to
来
<taxes>
<t0> E </t0>
</taxes>
for example
例如
#1
2
As mentioned in the comments the issue is with your number elements, <0>
. That is an invalid element name. You could manually modify the XML file if this is a static file, remove or rename, use a regex, or suppress the error. If you suppress you will have to remember that in the future, it will never tell you about errors.
正如评论中提到的,问题在于您的number元素<0>。这是一个无效的元素名。如果这是一个静态文件,您可以手动修改XML文件、删除或重命名、使用regex或抑制错误。如果你压抑自己,你必须记住,在未来,它永远不会告诉你错误。
Regex approach:
正则表达式的方法:
$invoice = preg_replace('~<(/?\d)~', '<number$1', $invoice);
Regex demo: https://regex101.com/r/pZ3sJ4/1
Regex演示:https://regex101.com/r/pZ3sJ4/1
One suppression option:
一个抑制选项:
libxml_use_internal_errors(true);
$domxml->loadXML($invoice);
libxml_clear_errors();
With this approach you can still retrieve the errors pretty easily.
使用这种方法,您仍然可以很容易地检索错误。
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();
Second suppression option:
第二个抑制选项:
@$domxml->loadXML($invoice);
yuck..
恶心. .
Third simple non-programming option, just edit the file manually:
第三个简单的非编程选项,只需手动编辑文件:
<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>
#2
2
Element names must start with a letter or underscore
元素名称必须以字母或下划线开头。
Watch http://www.w3schools.com/xml/xml_elements.asp for more information
更多信息请访问http://www.w3schools.com/xml/xml_elements.asp
change
改变
<taxes>
<0> E </0>
</taxes>
to
来
<taxes>
<t0> E </t0>
</taxes>
for example
例如