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 4025980
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:54:49+00:00 2026-05-20T10:54:49+00:00

I need to walk through a JDOM tree and extract all data from body

  • 0

I need to walk through a JDOM tree and extract all data from body section to use it in another xml document creation. I’m very new to programming. I have attached my concept and error shown in console. I want to clarify whether this concept is right or wrong. Could any body help me to know about this and give a direction?

Would appreciate any pointers..

//root- Existing document's root.
//body- New documents body.
private static Element listChildren(Element root, int depth) {
    System.out.println(root.getName());
    List children = root.getChild("body").getChildren();
    Iterator iterator = children.iterator();
    while (iterator.hasNext()) {
         Element child = (Element) iterator.next();
         System.out.println(child.toString());
         body.addContent(child);
         listChildren(child, depth+1);
         return child;
    }

    return null;
}

Error shown:

Exception in thread "main" java.lang.NullPointerException
    at createXhtml1.listChildren(createXhtml1.java:85)
    at createXhtml1.newXhtml(createXhtml1.java:62)
    at createXhtml1.main(createXhtml1.java:112)
  • 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-20T10:54:50+00:00Added an answer on May 20, 2026 at 10:54 am

    So based on comments your problem is with root.getChild("body"). This method returns null (there’s no child named body in root element). You should check for null and return null from method.

    ....
    Element element = root.getChild("body")    
    if (element == null)
        return null;
    List children = element.getChildren();
    ...
    

    EDIT
    According to comment, you can print (or whatever you want to do) all elements.

    public class Test {
        public static void main(String[] args) throws Exception {
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html> <body> <h1 align=\"center\">Profile</h1><hr /> <div class=\"centered\"> <table><tr><td><strong>Name: </strong></td> <td>A</td> </tr> <tr> <td><strong>Age: </strong></td> <td>23</td> <td>programmer</td></tr><tr><td><strong>Email: </strong></td><td>adfg@gmail.com</td></tr></table></div><hr /></body></html>";
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(new StringReader(xml));
            listChildren(doc.getRootElement(), 0);
        }
    
        private static void listChildren(Element root, int depth) {
            System.out.println(root.getName());
            List<Element> children = root.getChildren();
            if (children == null || children.isEmpty())
                    return;
            for (Element child : children) {
                     System.out.println(child.toString());
                     listChildren(child, depth+1);
            }
    
            return;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I want to use this API: http://hiveminder.com/help/reference/API.html The instructions walk through its
I am trying to load a html page through UIWebview.I need to disable all
in programming there is often such tasks walk through in multi dimensional array (for
I have the report with lots of sub-reports within. How can I walk through
The two posts below are great examples of different approaches of extracting data from
Need a function that takes a character as a parameter and returns true if
Need a way to allow sorting except for last item with in a list.
Need to an expression that returns only things with an I followed by either
Need to locate the following pattern: The letter I followed by a space then
need ask you about some help. I have web app running in Net 2.0.

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.