0

I have an XML file

<Entry>
    <uid>11111</uid>
    <firstName>Alex</firstName>
    <lastName>Stoun</lastName>
    <sdnType>Individual</sdnType>
    <idList>
      <id>
       <uid>2233</uid>
      </id>
    </idList>
    <akaList>
      <aka>
        <uid>3333</uid>
        <lastName>Stounne</lastName>
        <firstName>Aliks</firstName>
      </aka>
    </akaList>    
</Entry>    

Not all data from the XML file must get into the table.

The table has columns:

id | uid | fullName | sdnType | alias
@Override
public void parseSnFile(String fileName) {    

      //...

      try {
          //...
          NodeList list = doc.getElementsByTagName("Entry");

          List<someEntity> entity = new ArrayList<>();

      for (int temp = 0; temp < list.getLength(); temp++) {
             Node node = list.item(temp);
             if (node.getNodeType() == Node.ELEMENT_NODE) {
                 Element element = (Element) node;
                 String id = element.getElementsByTagName("uid").item(0).getTextContent();
                 String lastName = element.getElementsByTagName("lastName").item(0).getTextContent();
                 String firstName = element.getElementsByTagName("firstName").item(0).getTextContent();
                 String sdnType = element.getElementsByTagName("sdnType").item(0).getTextContent();
                 String programList = element.getElementsByTagName("program").item(0).getTextContent();

                 MyEntity entity = new MyEntity(id, lastName, snType, program); //serealize your data into entity
                 myRepo.save(entity) //saving to database
                 System.out.println(id + " " + lastName + " snType " + snType + " programList " + programList);

      someEntity entity = new someEntity(Long.parseLong(uid), firstName + lastName, *** );

      entity.add(someEntity);
      }
   }
   repository.saveAll(entity);

At this point I need to add alias data (in the XML file, these are firstName and lastName, which are inside the <akaList> tag)

Perhaps this can be done through a for loop, but I don’t know how to add this element to my table. There are more than 1000 entries in the XML file, and inside the <Entry> tag is the <akaList> tag. Both the <Entry> tag and the <akaList> tag have lastName and firstName.

How do I get this data to be written to different columns?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Alexander
  • 167
  • 1
  • 10
  • I don't understand how I can split the XML file data into columns. lastName and fullName in the tag must be in the - fullName column. lastName and fullName in tag - in column - alias – Alexander Sep 04 '21 at 08:49
  • Does this answer your question - https://stackoverflow.com/a/16364653/8534285 – Ajay Kumar Sep 04 '21 at 12:47
  • Unfortunately no. I need a solution. That is, I have an XML file in which there are tags with nested tags inside. I need the line with the tag in the database to match the line with the nested tag. Otherwise, the data in the table is not saved correctly. – Alexander Sep 04 '21 at 16:39
  • How about this - https://stackoverflow.com/questions/54767498/java-read-xml-file-and-put-specific-tags-into-list ? – Ajay Kumar Sep 04 '21 at 22:03

0 Answers0