0

I am working an XML string with some 8931 records. I want to access some tags perform some split functions and trim functions before saving it. I tried using simple loops but it is taking a long time(nearly 10 min). I tried doing it using java executor service to do the functions parallelly. But I don't know how many tags I am going to change beforehand. So I can't use inline Run tasks. I am trying to create a subclass that will handle the code that changes the file, but I am getting null pointer exception in subclass run method when getting the list of nodes with the desired tag. doc is a global variable. So it should have something in it before it is being accessed because I am assigning it before I call to submit. If there is any other way to do this efficiently please suggest.

private void normalize_xml(Request_return req_xml) {
System.out.println("In normalize_xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
    ExecutorService executor = Executors.newFixedThreadPool(10);
    db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(req_xml.soapxml));
    try {
        doc = db.parse(is);
        NodeList nList = doc.getElementsByTagName("b:recordCount");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            req_xml.set_count(Integer.parseInt(nNode.getTextContent()));
            System.out.println("count tag value:"+nList.getLength());
            System.out.println("Record count :"+Integer.parseInt(nNode.getTextContent()));                  
        }
        System.out.println("Starting id normalization");
    if(xf_local.hasid==true) {
            xf_local.dateFields+=","+"id";                          
        }
        attri = xf_local.dateFields.split(",");
        Runnable task[];
        for(l=0;l<attri.length;l++) { 
            RunnableImpl task_me = new Requests().new RunnableImpl();
            task_me.tagname=attri[l];
            executor.submit(task_me);
        }
        executor.shutdown();
    } catch (SAXException e) {
        System.out.println("Something went wrong with parser. "+e);
    } 
        System.out.println("Starting saving");
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("Z:/files/sample_xml.xml"));
        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Done");
} catch (ParserConfigurationException e1) {
    System.out.println("Parser Configuration Exception. "+e1);      
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (TransformerConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

private class RunnableImpl implements Runnable { 
String tagname="";

public void run() 
{ 
     System.out.println("Executing Task inside for thread: " + tagname);
        try {
            System.out.println("1");
            NodeList nList_id = doc.getElementsByTagName("c:"+tagname);
            System.out.println("2");
            for (int temp = 0; temp < nList_id.getLength(); temp++) {
                System.out.println(""+tagname+" tag value:"+temp);
                Node nNode = nList_id.item(temp);
                if(tagname.equals("id")) {
                    nNode.setTextContent(nNode.getTextContent().trim());
                }
                else {
                    nNode.setTextContent(nNode.getTextContent().split("T")[0]);                                                             
                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
} 
} 

This is the output I get

Response Code : 200
In normalize_xml
count tag value:1
Record count :8391
Starting id normalization
Starting saving
Executing Task inside for thread: endDate
Executing Task inside for thread: checkDate
java.lang.NullPointerException
java.lang.NullPointerException
Done

It is trying to change two date tags and split them at 'T' and then update the value in the XML. It has to do the same thing for 8391 times for each tag. Thank you

  • 1
    Not sure but maybe you can consider to use SAX parser instead of DOM, with its ContentHandler callbacks? It may require more code from you, but... it can be faster I guess... you may just try it. As a start point check this https://www.tutorialspoint.com/java_xml/java_sax_parser.htm and there are some simple demo code as well. – Vadim Sep 20 '18 at 17:42
  • @Vadim thats exactly the problem. Loading huge XML files into memory does not work - use a SAX parser instead – TMS Sep 20 '18 at 22:30

0 Answers0