0

My requirement is remove few xml nodes in xml file(File size is around 200 MB) and save it.I'm using DOMParser for this and able to read the file and removed the node and writing into xml file but when writing into xml file getting Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.

I'm getting outofmemory error at transformer.transform(source, result); Can anyone help me in this how to resolve above the error. What is the best parser in java to parse the xml files ? Is DOMParser good to parse large XML files ? Please suggest.It is Urgent.Thanks

DocumentBuilder docBuilder = null;
    File inputFile = new File("C:/Users/dvoota.ORADEV/Desktop/5277603.xml");
    try{
    DocumentBuilderFactory docBuilderFactory =
        DocumentBuilderFactory.newInstance();
   // docBuilderFactory.setExpandEntityReferences(false);
   // docBuilderFactory.setValidating(true);
  //  docBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

    docBuilder = docBuilderFactory.newDocumentBuilder();
       Document doc= docBuilder.parse(inputFile);
        Element table = doc.getDocumentElement();
        Node nlist= table.getElementsByTagName("G_JRNLSOURCE_ROWSET").item(0);
         table.removeChild(nlist);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
                        Transformer transformer = transformerFactory.newTransformer();

                        DOMSource source = new DOMSource(doc);
                        StreamResult result = new StreamResult(new File("C:/Users/dvoota.ORADEV/Desktop/5277603.xml"));
                        transformer.transform(source, result);
user2155454
  • 95
  • 3
  • 12
  • 2
    [Increase heap size in Java](https://stackoverflow.com/questions/1565388/increase-heap-size-in-java) – vanje Jul 28 '18 at 17:41
  • if you are editing the xml, you have to use the dom parser. The dom parser creates the object of the XML and loads in the memory. For that you have only option of increasing the heap memory. – Dinesh Jul 28 '18 at 18:30
  • Have you tried to use a SAX parser instead of the DOM parser? See: https://stackoverflow.com/questions/13847454/remove-multiple-nodes-from-an-xml-file-using-sax-and-java – Alexander Egger Jul 28 '18 at 19:39

0 Answers0