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

The Archive Base Latest Questions

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

I am new to JDOM, and am having trouble creating a document. The problem

  • 0

I am new to JDOM, and am having trouble creating a document. The problem is that I want
to be able to add Elements that do NOT have the “xmlns” attribute. I am using JDOM 1.1

All of the examples I have found show the output without the “xmlns”. Here is a simple
code fragment, along with its output:

      Namespace jwNS = Namespace.getNamespace("http://www.javaworld.com");
      Element myElement = new Element("article", jwNS);
      Document doc = new Document(myElement);
      myElement.addContent(new Element("title").setText("Blah, blah, blah"));

// serialize with two space indents and extra line breaks
try {
  //XMLOutputter serializer = new XMLOutputter("  ", true);
  XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
  serializer.output(doc, System.out);
}
catch (IOException e) {
  System.err.println(e);
}

Output:

<?xml version="1.0" encoding="UTF-8"?>
<article xmlns="http://www.javaworld.com">
  <title xmlns="">Blah, blah, blah</title>
</article>

What I want is to just have

<?xml version="1.0" encoding="UTF-8"?>
<article xmlns="http://www.javaworld.com">
  <title>Blah, blah, blah</title>
</article>

Can anyone tell me what I am doing wrong?

  • 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-27T09:27:06+00:00Added an answer on May 27, 2026 at 9:27 am

    Given your desired example:

    <?xml version="1.0" encoding="UTF-8"?>
    <article xmlns="http://www.javaworld.com">
      <title>Blah, blah, blah</title>
    </article>
    

    This means that all child elements of <article> have the same namespace as <article>, i.e. namespaces are inherited from parents to children. That means you need to specify jwNS for all of your child elements, i.e.

    myElement.addContent(new Element("title", jwNS ).setText("Blah, blah, blah"));
    

    When rendering the XML output, JDOM should then omit the explicit namespace from <title>, since it inherits it from <article>.

    By using just new Element("title"), you’re saying that you want no namespace on <title>, and so JDOm has to add an explicit xnmns="" attribute in order to override the inheritance of the jwNS namespace from the <article> parent.

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

Sidebar

Related Questions

I want to send an xml document that i have created using jdom api.
I have XML document that I parse with jDOM and then try to take
I have used jdom for xml validation against schema. The main problem there is
NEW EDIT: I have narrowed my problem to this - i have a view
New to Applets, I have never dealt with having to export the resources to
I have a process that parses an XML file using JDOM and xpath to
I have an xml document as a string without any namespace and I want
I am really new to XML and JDOM so I have a noob question,
New to Joomla and want to add the joomla username availability check. Just check
New to javascript/jquery and having a hard time with using this or $(this) to

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.