0

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?

Yannick Mussche
  • 316
  • 2
  • 12
  • If we knew the type of `eElements` and what package / library it came from, that would help. There are *many* XML parsing libraries for Java. – markspace May 09 '22 at 13:21
  • Here's a much better question with actual code, that got an answer: https://stackoverflow.com/questions/11947248/how-to-get-the-value-using-getelementsbytagname – markspace May 09 '22 at 13:22
  • I use DocumentBuilderFactory, those this answer your question? – Yannick Mussche May 09 '22 at 13:23
  • No it does not. Please post the code. – markspace May 09 '22 at 13:24
  • your link is not an answer to my question, though thank you. I do not have any code to post on this topic. I have only written the code for 1 order. (works perfect) I want tot start now for the edition, but there might be more than 1, so I do not know how to start my code, because I don't know how to check this. – Yannick Mussche May 09 '22 at 13:29

1 Answers1

0

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();
Andronicus
  • 25,419
  • 17
  • 47
  • 88
Yannick Mussche
  • 316
  • 2
  • 12