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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.