如何将动态XML中的嵌套标签转换为for循环?

时间:2018-08-30 05:53:34

标签: java xml xml-parsing jsoup

我想将动态xml文件转换为特定的文件格式。我可以使用jsoup解析器解析xml,但问题是我想解析嵌套的标签并将其放入for循环中。有没有办法做到这一点。附上以下示例以供参考

输入XML(示例)

                         <lineComponents>
                        <invoiceComponents>
                            <invoiceComponent>
                                <type></type>
                                <name></name>
                                <amount>16.00</amount>
                                <taxPercentage>0.00</taxPercentage>
                                <taxAmount>0E-8</taxAmount>
                            </invoiceComponent>
                        </invoiceComponents>
                        <acctComponents>
                            <acctComponent>
                                <componentCode>BASE</componentCode>
                                <glAccountNr></glAccountNr>
                                <baseAmount>10.00000</baseAmount>
                                <taxRate>0.00</taxRate>
                                <taxAmount>0.00000</taxAmount>
                                <totalAmount>10.00000</totalAmount>
                                <isVAT>No</isVAT>
                            </acctComponent>
                            <acctComponent>
                                <componentCode></componentCode>
                                <glAccountNr></glAccountNr>
                                <baseAmount>3.00000</baseAmount>
                                <taxRate>0.00</taxRate>
                                <taxAmount>0.00000</taxAmount>
                                <totalAmount>3.00000</totalAmount>
                                <isVAT>No</isVAT>
                            </acctComponent>
                            <acctComponent>
                                <componentCode>DISC</componentCode>
                                <glAccountNr></glAccountNr>
                                <baseAmount>-2.00000</baseAmount>
                                <taxRate>0.00</taxRate>
                                <taxAmount>0.00000</taxAmount>
                                <totalAmount>-2.00000</totalAmount>
                                <isVAT>No</isVAT>
                            </acctComponent>
                            <acctComponent>
                                <componentCode>ARPIT</componentCode>
                                <glAccountNr></glAccountNr>
                                <baseAmount>5.00000</baseAmount>
                                <taxRate>0.00</taxRate>
                                <taxAmount>0.00000</taxAmount>
                                <totalAmount>5.00000</totalAmount>
                                <isVAT>No</isVAT>
                            </acctComponent>
                        </acctComponents>
                    </lineComponents>

预期输出:

 for(OrderItem invoiceLineItem: orderLineWrp.invoiceLineItems){
            Dom.XMLNode invoiceComponentNode = invoiceComponentsNode.addChildElement(EP_OrderConstant.invoiceComponent,null,null);
            invoiceComponentNode.addChildElement(EP_OrderConstant.seqId,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_SeqId__c));
            invoiceComponentNode.addChildElement(EP_OrderConstant.TYPE,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_ChargeType__c));
            invoiceComponentNode.addChildElement(EP_OrderConstant.name,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Invoice_Name__c));
            invoiceComponentNode.addChildElement(EP_OrderConstant.amount,null,null).addTextNode(getValueforNode(invoiceLineItem.UnitPrice)); //Value for amount
            invoiceComponentNode.addChildElement(EP_OrderConstant.taxPercentage,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Tax_Percentage__c)); //Value for taxPercentage
            invoiceComponentNode.addChildElement(EP_OrderConstant.taxAmount,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Tax_Amount_XML__c)); //Value for taxAmount
        }

此Xml文件是动态的。有什么方法可以将动态XML文件处理为上述特定格式?

1 个答案:

答案 0 :(得分:-1)

Jsoup更适合HTML解析。

如果XML带有XSD / DTD,则应使用JAXB-generated classesunmarshaller来读取它。

否则,您可以对大型XML使用JAXP(如果文件很小,则使用DOMParser,XPath,或基于事件的SAXParser(但是不太容易使用))文件)。