Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Fragment Marshal w/o namespace

I'm using the JAXB_FRAGMENT property for my marshaller to marshal at the WorkSet level. The problem is that when I marshal it's giving the WorkSet element the xmlns attribute everytime. Is there a way to marshal so that it doesn't attach the xmlns attribute? Here's what my XML looks like.

    <Import>
        <WorkSets>
            <WorkSet xmlns="http://www.namespace.com">
                <Work>
                <Work>
                ...
                ..
                ...
            </WorkSet>
            <WorkSet xmlns="http://www.namespace.com">
                <Work>
                <Work>
                ...
            </WorkSet>
        </WorkSets>
    </Import>

Here's the code I'm using the create the above:

FileOutputStream fos = new FileOutputStream("import.xml");
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(fos);

JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
Marshaller m = jc.createMarshaler();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

writer.writeStartDocument();
writer.writeStartElement("Import");
writer.writeAttribute("xmlns","http://www.namespace.com");
writer.writeStartElement("WorkSets");

while(hasWorkSet){
m.marshal(workSet, writer)
}
writer.writeEndDocument();
writer.close();
like image 936
TyC Avatar asked Feb 15 '12 17:02

TyC


People also ask

What is JAXB fragment?

The -jaxb. fragment command determines whether the marshaller generates document-level events in the XML data. This command is optional. If you omit it, the default is false. Document-level events are not generated in the XML data.

What is JAXB marshalling and Unmarshalling?

JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.

Is JAXB Unmarshaller thread safe?

All other objects, including Marshaller and Unmarshaller, are not thread-safe and must not be shared. The static helper methods in the JAXB class can be used from several threads, of course. In practice, this means that if you need a JAXBContext instance, you should probably store in a static member.


2 Answers

Assuming you want the default namespace for your document to be http://www.namespace.com, you could do the following:

Demo

The XMLStreamWriter.setDefaultNamespace(String) and XMLStreamWriter.writeNamespace(String, String) methods will be used to set and write the default namespace for the XML document.

package forum9297872;

import javax.xml.bind.*;
import javax.xml.stream.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
        writer.setDefaultNamespace("http://www.namespace.com");

        JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        writer.writeStartDocument();
        writer.writeStartElement("http://www.namespace.com", "Import");
        writer.writeNamespace("", "http://www.namespace.com");
        writer.writeStartElement("WorkSets");

        m.marshal(new WorkSet(), writer);
        m.marshal(new WorkSet(), writer);

        writer.writeEndDocument();
        writer.close();
    }

}

WorkSet

My assumption is that you have specified namespace information in your JAXB model.

package forum9297872;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="WorkSet", namespace="http://www.namespace.com")
public class WorkSet {

}

Output

Below is the output from running the demo code:

<?xml version="1.0" ?><Import xmlns="http://www.namespace.com"><WorkSets><WorkSet></WorkSet><WorkSet></WorkSet></WorkSets></Import>
like image 111
bdoughan Avatar answered Oct 23 '22 00:10

bdoughan


There are three workarounds for this.

1) Create JAXB annotated objects for the container of your workersets. Add the workersets to that object and then marshal the whole thing.

2) Follow the first example in 101 ways to marshal objects with JAXB and use DocumentBuilderFactory with namespace aware.

3) Assuming that the jaxb object is in a package that should never have qualified namespaces you can add the following to the package annotation: (note: it's been a while since i've done this and I havn't tested this code)

@XmlSchema(namespace = "", elementFormDefault = XmlNsForm.UNQUALIFIED) 
package example;
like image 23
Daniel Moses Avatar answered Oct 22 '22 23:10

Daniel Moses