'Check the quantity of similar nodes in xml, parse them 1 by 1

My program reads in xml data to automatically fill in the orders in the database. This program is written in Java with jpa, since it builds on sets. When 1 client, orders 1 edition, for 1 delivery adres, I can easily parse the xml. But what if we would have multiple editions or delivery adresses? As an example, I here have an example of a possible xml file, offcourse without sensitive data:

<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>

I use this line of java code to get the specified information per node (offcourse I parse the entire xml in advance), which I than insert in my New Order () as the specified attribute :

eElements.getElementsByTagName("EditionId").item(0).getTextContext();

My question is, when I have multiple items or delivery adresses, how do instruct my program to check this?

in human language I want it do this:

  1. check wheter there is only 1 item node or multiple
  2. if only one, no problem
  3. if multiple, create a line for each item

And offcourse the same for delivery adresses. But since their is only 1 contact per item, in this case you cannot see in the xml. In some of the xml's, they alse have multiple contacts within the item. But I guess this will be exactly the same method as for multiple items within orders.

What is the best way to instruct this?



Solution 1:[1]

To answer the first part, there is a getLength attribute available in documentBuilderFactory.

var nodeList = eElement.getElementsByTagName("EditionId");
editielijst.getLength());

the second part is use a loop -1 and than fill in

var EditionId1 = eElement.getElementsByTagName("EditionId").item(0).getTextContent();
var EditionId2 = eElement.getElementsByTagName("EditionId").item(1).getTextContent();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andronicus