Apache XMLBeans – output XML without a namespace

This is again something that I need to know how to do but never remember how when time it tight.

Apache XMLBeans http://xmlbeans.apache.org/ is a great tool for working with XML in Java, but it requires the XML Schema being used to create the objects from XML to have a Namespace. The namespace ends up being part of the package structure for the Objects created and I guess having a unique path for these is a good idea. However, this requirement can be a bit of a pain, when working with some simple XML structures that do not have a namespace. Especially when your ready to persist the XML Bean objects to XML source. If a namespace is added to your XML Schema that the XML Bean objects are created from, XML source generated from them will by default also have the namespace. I can never remember how to output the XML source without a namespace and so I am writing it down here where I can get at it with a click, and maybe this will help someone else as well.

There are two key steps to remove the Namespace when outputting XML Source.

  1. Tell it to use the default namespace:
  2. Tell it that you have already declared the default namespace:
           dnsMap.put("", "http://example.com/schemas/DefaultNnameSpace");

After this, you can output as normal:

      retString = myXMLDoc.xmlText(xmlops);
      return retString;

Note: the research and testing to solve this was done using XMLBeans v2.4.0

About Jayson
Father, mountain biker, Software development manager at a wire service, and also work with the IPTC (iptc.org)

8 Responses to Apache XMLBeans – output XML without a namespace

  1. Dan says:

    I was surprised at how difficult it was to find a solution to this exact problem. Thanks so much for posting this.

    • Jayson says:

      Dan, I hear ya. One would think something like that would be super easy to find but it just was not. Not a hard thing, but hard to track down. I think I finally found something in the Apache docs but it was not right there on the top of anything for sure.

  2. Petri says:

    Jayson, thank you so much for posting this. I was growing desperate to solve this but luckily stumbled on your solution.


    • Jayson says:

      Hear ya, it really should be easier to control this behavior or at least be better documented. Some people in our office had given up completely and their production code was generating XML and then parsing again to remove the namespace.

  3. Thanks for you post..solved my problem.

  4. nsahaytech says:

    I tried using the same code but the namespace is not getting removed. I’m using xbean version 2.4. Any insight into the issue would be of great help
    XmlOptions xmlOptions = new XmlOptions();
    HashMap dnsMap = new HashMap();
    dnsMap.put(“”, “http://example.com/schemas/DefaultNnameSpace”);
    String retString = msgBody.xmlText(xmlOptions);
    System.out.println(“String xml”+retString);

  5. Joe says:

    Wow…..I realize I’m new to Java , but this still took a long time to implement! Thank you kindly sir for sharing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: