Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming you want the default namespace for your document to be <code>http://www.namespace.com</code>, you could do the following:</p> <p><strong>Demo</strong></p> <p>The <code>XMLStreamWriter.setDefaultNamespace(String)</code> and <code>XMLStreamWriter.writeNamespace(String, String)</code> methods will be used to set and write the default namespace for the XML document.</p> <pre><code>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(); } } </code></pre> <p><strong>WorkSet</strong></p> <p>My assumption is that you have specified namespace information in your JAXB model.</p> <pre><code>package forum9297872; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="WorkSet", namespace="http://www.namespace.com") public class WorkSet { } </code></pre> <p><strong>Output</strong></p> <p>Below is the output from running the demo code:</p> <pre><code>&lt;?xml version="1.0" ?&gt;&lt;Import xmlns="http://www.namespace.com"&gt;&lt;WorkSets&gt;&lt;WorkSet&gt;&lt;/WorkSet&gt;&lt;WorkSet&gt;&lt;/WorkSet&gt;&lt;/WorkSets&gt;&lt;/Import&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload