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

问题描述 投票:0回答:1

我想将动态xml文件转换为特定的文件格式。我能够使用jsoup解析器解析xml,但问题是我想解析嵌套的标签并将其放入for-loop.Is有任何方法可以做到这一点。附上以下样本供参考

输入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文件处理成上面的特定格式?

java xml xml-parsing jsoup
1个回答
-1
投票

Jsoup更适合HTML解析。

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

否则你可以使用JAXP(DOMParser,如果文件很小,XPath,或基于事件的SAXParser(但这不是那么容易使用),对于非常大的XML文件)。

© www.soinside.com 2019 - 2024. All rights reserved.