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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:03:05+00:00 2026-06-05T15:03:05+00:00

I am trying to build an XML document using a specific namespace. The final

  • 0

I am trying to build an XML document using a specific namespace. The final document I am trying to generate is supposed to look like this:

<m:documentObject xmlns:m="http://www.myschema.com">
    <sender>token</sender>
    <receiver>token</receiver>
    <payload>token</payload>
</m:documentObject>

Here is what i have so far.

Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element requestElement = document.createElementNS("http://www.myschema.com", "documentObject");

document.appendChild(requestElement);

Element sender = document.createElement("sender");
requestElement.appendChild(sender);     
Text senderText = document.createTextNode("Xmlsender");
sender.appendChild(senderText); 

Element receiver = document.createElement("receiver");
requestElement.appendChild(receiver);       
Text receiverText = document.createTextNode("Xmlreceiver");
receiver.appendChild(receiverText); 

Element payload = document.createElement("payload");
requestElement.appendChild(payload);        
Text payloadText = document.createTextNode("Xmlpayload");
payload.appendChild(payloadText);   

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);

DOMSource source = new DOMSource(requestElement);

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); 
transformer.transform(source, result);

String xmlString = sw.toString();
System.out.println(xmlString)

For some reason when I run the above the schema comes out without the prefix. As shown below:

<?xml version="1.0" encoding="utf-8"?>
<documentObject xmlns="http://www.myschema.com">
    <sender>Xmlsender</sender>
    <receiver>Xmlreceiver</receiver>
    <payload>Xmlpayload</payload>
</documentObject>

What do I need to do so that XML is exactly as shown in the first XML example with the namespace prefix and the tags to have the namespace prefix?

I am trying to create an XML string which will be used for a Spring-WS webservice which expects a JAXB object which is in the format shown in the first example.

  • 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-05T15:03:07+00:00Added an answer on June 5, 2026 at 3:03 pm

    You can use setPrefix.

    But it is better to create the root element like this:

    document.createElementNS("http://www.myschema.com", "m:documentObject");
    

    Note also that passing null to createElement is a supported way of forcing a null namespace. In your original example this would however not work because your document element effectively forces a default namespace by combining a namespace URI with no prefix.

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

Sidebar

Related Questions

I am trying to build a XML document using the GML namespace and XML
I am trying to build a list that parses my entire xml document. I
I am trying to build a offline wiktionary using the wikimedia dump files (.xml.bz2)
I'm trying to build a rather complex XML document. I have a bunch of
I'm trying to build an XDocument that requires two declarations. The final document needs
I'm using an XDocument to build an Xml document in a known structure. The
I am trying to build an XML document in C# with CDATA to hold
I am trying to read the XML Documentation file (C#) using this ocde -
I'm trying to build xml for a library I'm making. What I'm trying to
I'm trying to better integrate Eclipse with my build.xml. My build file calls GNU

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.