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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:48:25+00:00 2026-05-24T06:48:25+00:00

Lets consider this xml file : <?xml version=1.0 encoding=UTF-8?> <root attribute=value> <element>myElement</element> </root> I’m

  • 0

Lets consider this xml file :

<?xml version="1.0" encoding="UTF-8"?>
<root attribute="value">
    <element>myElement</element>
</root>

I’m trying to parse the file using JDOM to extract the attribute name and value from the root element.

This is the code I mad for this purpose :

public static org.jdom.Document document;
public static org.jdom.Element root;

SAXBuilder sxb = new SAXBuilder();
try
{
    document = sxb.build(new File("file.xml"));
    root = document.getRootElement();

    List myList = root.getAttributes();
    Iterator x = myList.iterator();
    while(x.hasNext())
    {
        Attribute myAttribute = (Attribute)x.next();
        System.out.println("name : " +  myAttribute.getName() + " & value : " + myAttribute.getValue());
    }
}
catch(Exception e){
    e.printStackTrace();
}

Everything works fine for this xml file. But when I use this file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rootElement PUBLIC "-//Project" "mydtd.dtd">
<root attribute="value">
    <element>myElement</element>
</root>

I get Exception in thread "main" java.lang.NullPointerException

Do you think I should remove the DOCTYPE from the xml file before parsing it and past it after I finish the parsing.

Or there is something else I can do in this situation ?

Thanks

  • 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-24T06:48:25+00:00Added an answer on May 24, 2026 at 6:48 am

    Or there is something else I can do in this situation ?

    Yes – never, ever do this:

    catch(Exception e){}
    

    This is not a problem with JDOM or attributes, this is a problem with your general exception handling.

    It’s likely that you’re getting a NullPointerException because the XML file didn’t parse properly, so the document field is null. But because you caught and ignored the exception, you get no meaningful error message.

    Move the all of the code you currently have after the catch block, inside the try/catch block, and you’ll get a more useful error report.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider the following XML Schema: <?xml version=1.0 encoding=UTF-8?> <schema targetNamespace=http://www.example.org/library elementFormDefault=qualified xmlns=http://www.w3.org/2001/XMLSchema xmlns:lib=http://www.example.org/library>
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Lets consider some cases: _windsor.Register(Component.For<IProductServices>().ImplementedBy<ProductServices>().Interceptors(typeof(SomeInterceptorType)); In this case, when I ask for a IProductServices
This is a conceptual question involving Hadoop/HDFS. Lets say you have a file containing
Let's consider this sample example text file: Level: 1 Level: 1 Level: 2 Level:
Lets consider default ASP.NET MVC application folder structure, so it's looks like this: -App_data
I'm writing a module that has several methods. Let's consider this : package MyPackage;
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider: print $foo, AAAAAAAA, $foo, BBBBBBBB; Let's say I want to use this code
Lets consider that I have a public property called AvatarSize like the following, public

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.