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

  • Home
  • SEARCH
  • 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 6801601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:07:13+00:00 2026-05-26T19:07:13+00:00

If there is an XML file called a.xml , is there any way to

  • 0

If there is an XML file called a.xml, is there any way to traverse its DOM tree in
postorder fashion?

I tried using GetNextSiblings method but it didn’t work. Any idea?

Here is the XML:

<?xml version="1.0" encoding="UTF-8"?> 
       <title text="title1"> 
           <comment id="comment1">
               <data> abcd </data>
               <data> efgh </data>
           </comment>
           <comment id="comment2">
               <data> ijkl </data>
               <data> mnop </data>
               <data> qrst </data>
           </comment>
       </title>

And here is my code to traverse it:

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;

public class Newtraverse {
public static Node check(Node node){
    Node c=node;
    // Node c = null;
    if (node!=null)
        if (node.hasChildNodes()==true &&node.getNodeName()!=null)
        {
            node=node.getFirstChild().getNextSibling();
            if (node!=null)
            {
                 System.out.println(node);

                 check(node); 
            }

            if(node==null)
            {
                c=c.getNextSibling();
                check(c);
            }
       }

    return node;
 }
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        File file = new File("d:\\a.xml");
  DocumentBuilderFactory dbf =
  DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document document = db.parse(file);
  document.getDocumentElement().normalize();


  Node b=document.getFirstChild();

 Node result= check(b);
}
}

And here is the output:

[comment: null]
[data: null]

As you all can see, it just traverses two tags. How can I fix this?

  • 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-26T19:07:13+00:00Added an answer on May 26, 2026 at 7:07 pm

    Here’s how your check method should like (I didn’t run it though…)

    public static void check(Node node){
      if (node == null || node.getNodeName() == null)
        return;
    
      // Do PostOrder on all children
      check(node.getFirstChild());  
    
      // Now that all children were traversed, process the current node:
      System.out.println(node); 
    
      // Do PostOrder on following siblings
      check(node.getNextSibling();  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to strip elements out of a web.xml file using ANT?
Is there any PHP script to convert XML file to DTD. I tried a
Is there any other place besides the metabase.xml file where the file upload size
Is there a way to validate an XML file against a DTD with Qt's
Is there an easy way with LINQ to flatten an XML file? I can
Is there a way to read off from an [external] xml (an xml file
Is there any way to inherit settings in the .config file from a subassembly?
say there is an xml file, which not created by me, with a known
Is there a ColdFusion analog for the deployment descriptor/web.xml file found in a J2EE
I have created XML file,but I can't view it/output it.I know there is no

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.