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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:14:26+00:00 2026-06-06T15:14:26+00:00

I am creating a W3C Document object using a String value. Once I created

  • 0

I am creating a W3C Document object using a String value. Once I created the Document object, I want to add a namespace to the root element of this document. Here’s my current code:

Document document = builder.parse(new InputSource(new StringReader(xmlString)));
document.getDocumentElement().setAttributeNS("http://com", "xmlns:ns2", "Test");
document.setPrefix("ns2");
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(document);
Result dest = new StreamResult(new File("c:\\xmlFileName.xml"));
aTransformer.transform(src, dest);

What I use as input:

<product>
    <arg0>DDDDDD</arg0>
    <arg1>DDDD</arg1>
</product>

What the output should look like:

<ns2:product xmlns:ns2="http://com">
    <arg0>DDDDDD</arg0>
    <arg1>DDDD</arg1>
</ns2:product>

I need to add the prefix value and namespace also to the input xml string. If I try the above code I am getting this exception:

NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

Appreciate your help!

  • 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-06-06T15:14:29+00:00Added an answer on June 6, 2026 at 3:14 pm

    Since there is not an easy way to rename the root element, we’ll have to replace it with an element that has the correct namespace and attribute, and then copy all the original children into it. Forcing the namespace declaration is not needed because by giving the element the correct namespace (URI) and setting the prefix, the declaration will be automatic.

    Replace the setAttribute and setPrefix with this (line 2,3)

    String namespace = "http://com";
    String prefix = "ns2";
    // Upgrade the DOM level 1 to level 2 with the correct namespace
    Element originalDocumentElement = document.getDocumentElement();
    Element newDocumentElement = document.createElementNS(namespace, originalDocumentElement.getNodeName());
    // Set the desired namespace and prefix
    newDocumentElement.setPrefix(prefix);
    // Copy all children
    NodeList list = originalDocumentElement.getChildNodes();
    while(list.getLength()!=0) {
        newDocumentElement.appendChild(list.item(0));
    }
    // Replace the original element
    document.replaceChild(newDocumentElement, originalDocumentElement);
    

    In the original code the author tried to declare an element namespace like this:

    .setAttributeNS("http://com", "xmlns:ns2", "Test");
    

    The first parameter is the namespace of the attribute, and since it’s a namespace attribute it need to have the http://www.w3.org/2000/xmlns/ URI. The declared namespace should come into the 3rd parameter

    .setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns2", "http://com");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(creating a separate question after comments on this: Javascript redeclared global variable overrides old
Creating a simple RPG game, first time using XNA. Trying to get my character
When creating a view and setting group by value some field, it groups only
I am trying to add or connect multiple sql queries dynamically. I am creating
For creating rounded buttons, I am using three images a left image, a right
I create my pdf document out of a html File. I want to save
I'm creating a web application which loads tabs using AJAX. My problem is that
I'm creating a responsive website using several stlyesheets. Everything works fine in IE 9,
I'd like to open an HTML document (as a string retrieved from a StreamReader,
I'm creating a Disqus notifier Chrome extension. This involves making an HTTP call 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.