Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:00:44+00:00 2026-05-27T21:00:44+00:00

I am using the following code for parsing of small xml files and it

  • 0

I am using the following code for parsing of small xml files and it is working successfully. But when I parse huge data files I am getting a stack overflow error. So, I decided to convert this method into an iterative style. Initially when writing this method, I created the logic and wrote it successfully though when converting to an iterative style I’ve become completely lost and I’m not getting the required output. This is my recursive code:

private void xmlParsing(Node node,int indent) throws IOException {
    if (node.hasChildNodes()) {
        Node firstChild=node.getFirstChild();
        xmlParsing(firstChild,indent+1);
    } else {
        System.out.println(node.getNodeName()+":"+node.getNodeValue()+":"+indent);
    }
    Node nextNode=node.getNextSibling();
    if (nextNode!=null) {
        xmlParsing(nextNode,indent);
    }
}

Can someone please help me to convert this to iterative function that will perform this logic under single function? I hope I have made a clear request.

My full code:

package sample;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class NewTestClass {

    private Document doc = null;

    public NewTestClass() {
        BufferedWriter br=null;
        try {
            doc = parserXML(new File("debug.xml"));

            br=new BufferedWriter(new FileWriter("xmldata.txt"));
            xmlParsing(doc, 0,br);
        } catch(Exception error) {
            error.printStackTrace();
        } finally {
            try {
                br.flush();
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    private void xmlParsing(Node node,int indent,BufferedWriter br) throws IOException {
        if (node.hasChildNodes()) {
            Node firstChild=node.getFirstChild();
            xmlParsing(firstChild,indent+1,br);
        } else {

            br.write(node.getNodeName()+":"+node.getNodeValue()+":"+indent);
            br.newLine();
        }
        Node nextNode=node.getNextSibling();
        if (nextNode!=null) {
            xmlParsing(nextNode,indent,br);
        }
    }

    public Document parserXML(File file) throws SAXException, IOException, ParserConfigurationException
    {
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
    }

    public static void main(String[] args)
    {
        new NewTestClass();
    }
}

My initial error:

Exception in thread "main" java.lang.StackOverflowError
    at com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.getNodeValueString(Unknown Source)
    at com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.getNodeValueString(Unknown Source)
    at com.sun.org.apache.xerces.internal.dom.DeferredTextImpl.synchronizeData(Unknown Source)
    at com.sun.org.apache.xerces.internal.dom.CharacterDataImpl.getNodeValue(Unknown Source)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:00:45+00:00Added an answer on May 27, 2026 at 9:00 pm

    Your problem is the fact that you recurse also for siblings, not only for children. Child recursion is perfectly OK, but in your case, recursion goes as deep as the number of (flattened) nodes (not only elements) in your document.

    Do this instead:

    private void xmlParsing(Node node, int indent) throws IOException {
    
        // iterate for siblings
        while (node != null) {
    
            // recurse for children
            if (node.hasChildNodes()) {
                Node firstChild = node.getFirstChild();
                xmlParsing(firstChild, indent + 1);
            } else {
                // do the leaf node action
            }
    
            node = node.getNextSibling();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following code for other JSON data its working but here it
I'm using following code but cannot return data from MySQL. This is the output:
I am using the following code for parsing dom document but at the end
I have the following XML Parsing code in my application: public static XElement Parse(string
I'm parsing a XML using jQuery with the following code: function appendNav(xml) { $(xml).find(Nav).each(function()
I am using the following code by the Android-people-XMLParsing example for parsing XML file
Im using the following code to output all the nodes from the xml shown.
I'm using the following code with the javax.swing.text.html.parser.ParserDelegator in order to parse hyperlinks from
I am parsing XML in PHP code and getting a warning invalid argument supplied
In the following code, I am trying to do a text parsing by using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.