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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:27:22+00:00 2026-05-24T07:27:22+00:00

Question How to eliminate auto-generated namespace prefix that appears on all elements and attributes

  • 0

Question

How to eliminate auto-generated namespace prefix that appears on all elements and attributes when using JAXB marshalling

I’ve demonstrated my current XML output after marshalling and the expected output.

Details

I’m using the default JaxB implementation (Metro) provided with JDK 1.6 update 21.

My XSD file is shown below. I used xjc to generate the Java Classes for this XSD and I dont want to add/change any
annotations in the generated Java classes, so that I can continue to use xjc.

In the code, this is how I marshal….where I create MYJAVAOBJECTTREE using ObjectFactory etc..

    JAXBContext jcDXD = JAXBContext.newInstance(MDASJ.class);
    QName qn=new QName(XMLDataFormat.XML_ROOT_NAME);
    marshallerDXD = jcDXD.createMarshaller();
    marshallerDXD.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshallerDXD.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
    marshallerDXD.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.theronyx.com/mdasj/xmldata mdasj-data.xsd");
    jaxbElementDXD = new JAXBElement<MDASJ>(qn, MDASJ.class, MYJAVAOBJECTTREE);
    marshallerDXD.marshal(jaxbElementDXD, System.out);

XSD File

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                targetNamespace="http://www.theronyx.com/mdasj/xmldata" xmlns="http://www.theronyx.com/mdasj/xmldata">


    <!-- definition of attributes -->
    <xs:attribute name="ID" type="xs:string"/>
    <xs:attribute name="ComputerTime" type="xs:string"/>
    <xs:attribute name="VarId" type="xs:string"/>
    <xs:attribute name="Value" type="xs:string"/>
    <xs:attribute name="DataType" type="xs:string"/>

    <!-- definition of complex elements -->

    <!-- DIH -->
    <xs:element name="DIH">
      <xs:complexType>
        <xs:attribute ref="ID" use="required"/>
      </xs:complexType>
    </xs:element>

    <!-- TimeStamp -->
    <xs:element name="TimeStamp">
      <xs:complexType>
        <xs:attribute ref="ComputerTime" use="required"/>
      </xs:complexType>
    </xs:element>

    <!-- Variable -->
    <xs:element name="Variable">
      <xs:complexType>
        <xs:attribute ref="VarId" use="required"/>
        <xs:attribute ref="Value" use="required"/>
        <xs:attribute ref="DataType" />
      </xs:complexType>
    </xs:element>



    <!-- Root Data Spec -->
    <xs:element name="MDASJ">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="DIH"/>
          <xs:element ref="TimeStamp"/>
          <xs:element ref="Variable"  maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute ref="ID" use="required"/>
      </xs:complexType>
    </xs:element>

    </xs:schema>

Current XML File Output

    <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <MDASJ ns1:ID="MDASJID" xsi:schemaLocation="http://www.theronyx.com/mdasj/xmldata mdasj-data.xsd" xmlns:ns1="http://www.theronyx.com/mdasj/xmldata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ns1:DIH ns1:ID="servo1"/>
        <ns1:Variable ns1:DataType="Numeric" ns1:Value="0.19830813342577691127388561653788201510906219482421875" ns1:VarId="key1"/>
        <ns1:Variable ns1:DataType="Text" ns1:Value="-3815206174054821329" ns1:VarId="key2"/>
    </MDASJ>

Desired XML File Output is

    <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <MDASJ ID="MDASJID" xsi:schemaLocation="http://www.theronyx.com/mdasj/xmldata mdasj-data.xsd" xmlns="http://www.theronyx.com/mdasj/xmldata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <DIH ID="servo1"/>
        <Variable DataType="Numeric" Value="0.19830813342577691127388561653788201510906219482421875" VarId="key1"/>
        <Variable DataType="Text" Value="-3815206174054821329" VarId="key2"/>
    </MDASJ>
  • 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-24T07:27:23+00:00Added an answer on May 24, 2026 at 7:27 am

    You can use a NamespacePrefixMapper

    marshallerDXD.setProperty("com.sun.xml.bind.namespacePrefixMapper",
                              myNsPrefixMapper);
    

    to have control for the namespace prefixes:

    public class MyNsPrefixMapper extends NamespacePrefixMapper
    {
      public String getPreferredPrefix(String uri, String suggest, boolean require)
      {
        if("http://www.theronyx.com/mdasj/xmldata".equals(uri) ){return "";}
        return suggest;
      }
    
      public String[] getPreDeclaredNamespaceUris()
      {
        // String[] result = new String[1];
        // result[0] = "http://www.theronyx.com/mdasj/xmldata";
        return new String[0];
      }
    }
    

    I’ve tested the marshalling with:

    MDASJ xml = ....;
    JAXBContext context = JAXBContext.newInstance(MDASJ.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
                         "http://www.theronyx.com/mdasj/xmldata mdasj-data.xsd");
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper",new MyPrefixMapper());
    m.marshal(xml, System.out); 
    

    and this JAXB implementation:

    <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>2.2.2</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was curios about the question: Eliminate consecutive duplicates of list elements , and
Question Alright, I'm confused by all the buzzwords and press release bingo going on.
Question in the title. And what happens when all 3 of $_GET[foo] , $_POST[foo]
Question Can I build a image database/library that has an e-commerce style checkout system
Question says it all, really. My application is a time tracker. It's currently written
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question Is there a way to have a method that will always run anytime
My intuitive response to this question would be ,This is so stupid that I
Suppose I have a class that represents a product to be priced using one
Question Hello All, I've had some confusion for quite some time with essentially flooring

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.