How do I add attribute in XML node faster for more than 10000 record?(java)

I have to add an attribute to XML node with more than 10k records so whats the best way to transform XML document faster.

I have tried StAX parser which almost takes 4 min for adding an attribute and using SAX parser it should take 5 min.

Is there any other lib available to do it better or another way to do that please give your suggestions.

Sample Code :(Using STAX Parser)
XMLStreamReader r = factory.createXMLStreamReader(new FileInputStream(inputfile));

/*Start Writing document */

XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();

XMLEventWriter xmlEventWriter = xmlOutputFactory.createXMLEventWriter(new FileOutputStream(outputfile), "UTF-8");

/*End Writing document */

int event = r.getEventType();

long startTime = System.currentTimeMillis();

        System.out.println("Started reading node from xml document....."+TimeUnit.MILLISECONDS.toSeconds(startTime));

int node1Cnt = 0,node2Cnt = 0,node3Cnt=0,node4Cnt = 0;

      while (true) {

      XMLEventFactory eventFactory = XMLEventFactory.newInstance();

            switch (event) {

            case XMLStreamConstants.START_DOCUMENT:

                 // System.out.println("Start Document.");

                  StartDocument startDocument = eventFactory.createStartDocument();

                  xmlEventWriter.add(startDocument);

                  break;

            case XMLStreamConstants.START_ELEMENT:

                              

                //Create Start node

                  if(r.getLocalName().equalsIgnoreCase(node1)) {

                  node1Cnt++;

                  node2Cnt = 0;

                  Attribute attribute = eventFactory.createAttribute("id", "5522"+node1Cnt);

                  List attributeList = Arrays.asList(attribute);

                  List nsList = Arrays.asList();

                  StartElement sElement = eventFactory.createStartElement("", "", r.getLocalName(),attributeList.iterator(), nsList.iterator());

                  xmlEventWriter.add(sElement);

                  }else if(r.getLocalName().equalsIgnoreCase(node2Cnt)) {

                  node2Cnt++;

                  Attribute attribute = eventFactory.createAttribute("id", "5522"+node1Cnt+node2Cnt);

                  List attributeList = Arrays.asList(attribute);

                  List nsList = Arrays.asList();

                  StartElement sElement = eventFactory.createStartElement("", "", r.getLocalName(),attributeList.iterator(), nsList.iterator());

                  xmlEventWriter.add(sElement);

                  }else {

                  StartElement sElement = eventFactory.createStartElement("", "", r.getLocalName());

                  xmlEventWriter.add(sElement);

                  }

                  //xmlEventWriter.add(eventFactory.createAttribute("id", "5522"));

                  //xmlEventWriter.add(eventFactory.createDTD("\t"));

                 // xmlEventWriter.add(eventFactory.createDTD("\n"));

            StartElement sElement = eventFactory.createStartElement("", "", r.getLocalName());

                  xmlEventWriter.add(sElement);

                  break;

            case XMLStreamConstants.CHARACTERS:

                  if (r.isWhiteSpace())      break;     //System.out.println("Text: " + r.getText());                   

                  Characters characters = eventFactory.createCharacters(r.getText());

                  xmlEventWriter.add(characters);

                  break;

            case XMLStreamConstants.END_ELEMENT:

            //System.out.println("End Element:" + r.getName());

            EndElement endElement = eventFactory.createEndElement("", "", r.getLocalName());

                 // xmlEventWriter.add(eventFactory.createDTD("\t"));

                 // xmlEventWriter.add(eventFactory.createDTD("\n"));

                  xmlEventWriter.add(endElement);

                  break;

            case XMLStreamConstants.END_DOCUMENT:

            //System.out.println("End Document.");

            xmlEventWriter.add(eventFactory.createEndDocument());

                  break;

            }

            

            if (!r.hasNext())

                  break;

            event = r.next();

      }

      r.close();

      System.out.println("Ended reading node from xml document....."+(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())-TimeUnit.MILLISECONDS.toSeconds(startTime)));

} catch (XMLStreamException ex) {

ex.printStackTrace();

} catch (IOException ex) {

// TODO Auto-generated catch block

ex.printStackTrace();

}finally {

      System.out.println("finish!!");

}

Open in new window

vimal darjiJava DeveloperAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Your code is incorrect. It should be something like this:

            while (r.hasNext()) {

                event = r.next();

                switch (event) {
....

Open in new window

When i say "incorrect", i mean you're creating an XMLEventFactory 10,000 times the number of nodes (inc. attributes) in each record. That will take a huge hit
1
 
vimal darjiJava DeveloperAuthor Commented:
thanks, for your quick response. I have found my mistake XMLEventFactory eventFactory = XMLEventFactory.newInstance(); should not be in loop.
0
 
vimal darjiJava DeveloperAuthor Commented:
thanks, for your quick response. I have found my mistake XMLEventFactory eventFactory = XMLEventFactory.newInstance(); should not be in loop.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.