doutui7955 2016-02-12 14:37
浏览 695
已采纳

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

This is my 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?

Edit:

Here is the dynamic array before converted into xml using XMLSimpleElement, is there anyway to remove <0> before converted into 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
                                )

                        )

                )

        )

)
  • 写回答

2条回答 默认 最新

  • dpsr1670 2016-02-12 15:09
    关注

    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.

    Regex approach:

    $invoice = preg_replace('~<(/?\d)~', '<number$1', $invoice);
    

    Regex demo: 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?