[英]Check the quantity of similar nodes in xml, parse them 1 by 1
我的程序讀入xml數據自動填寫數據庫中的訂單。 這個程序是用 Java 和 jpa 編寫的,因為它是建立在集合上的。 當 1 個客戶訂購 1 個版本時,對於 1 個送貨地址,我可以輕松解析 xml。但是如果我們有多個版本或送貨地址怎么辦? 例如,我這里有一個可能的 xml 文件示例,當然沒有敏感數據:
<Order>
<Id>id</Id>
<Platform>platform</Platform>
<Brand></Brand>
<Orderdate>2022-03-14T18:34:28+01:00</Orderdate>
<Items>
<Item>
<Paper>
<EditionId>editionnr</EditionId>
<Name>nameOfEdition</Name>
<Platform>platform</Platform>
<Format>
<Name>Tabloid</Name>
</Format>
<NumberOfPages>x</NumberOfPages>
<PaperKind>paperkind</PaperKind>
<File name="xxxxxx"/>
</Paper>
<Contacts>
<Contact>
<Copies>x</Copies>
<Company>companyName</Company>
<Salutation></Salutation>
<Firstname>firstname</Firstname>
<Lastname>lastname</Lastname>
<Street>street</Street>
<HouseNumber>nr<HouseNumber/>
<BoxNumber/>
<Zip>xxxx</Zip>
<City>city</City>
<Country>countrytag</Country>
<C_Email>mail</C_Email>
</Contact>
</Contacts>
</Item>
<Item>
<Paper>
<EditionId>editionnr</EditionId>
<Name>nameOfEdition</Name>
<Platform>platform</Platform>
<Format>
<Name>Tabloid</Name>
</Format>
<NumberOfPages>x</NumberOfPages>
<PaperKind>paperkind</PaperKind>
<File name="xxxxxx"/>
</Paper>
<Contacts>
<Contact>
<Copies>x</Copies>
<Company>companyName</Company>
<Salutation></Salutation>
<Firstname>firstname</Firstname>
<Lastname>lastname</Lastname>
<Street>street</Street>
<HouseNumber>nr<HouseNumber/>
<BoxNumber/>
<Zip>xxxx</Zip>
<City>city</City>
<Country>countrytag</Country>
<C_Email>mail</C_Email>
</Contact>
</Contacts>
</Item>
</Items>
</Order>
我使用這行 java 代碼來獲取每個節點的指定信息(當然我提前解析了整個 xml),然后我將其作為指定屬性插入到我的 New Order () 中:
eElements.getElementsByTagName("EditionId").item(0).getTextContext();
我的問題是,當我有多個項目或送貨地址時,如何指示我的程序進行檢查?
用人類語言我希望它這樣做:
當然,送貨地址也一樣。 但由於每個項目只有 1 個聯系人,在這種情況下,您無法在 xml 中看到。在某些 xml 中,他們在項目中也有多個聯系人。 但我想這與訂單中的多個項目的方法完全相同。
指導這個的最好方法是什么?
要回答第一部分, documentBuilderFactory
中有一個getLength
屬性可用。
var nodeList = eElement.getElementsByTagName("EditionId");
editielijst.getLength());
第二部分是使用循環 -1 然后填寫
var EditionId1 = eElement.getElementsByTagName("EditionId").item(0).getTextContent();
var EditionId2 = eElement.getElementsByTagName("EditionId").item(1).getTextContent();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.