When I use the annotation:
@XmlRootElement(name="RootElement", namespace="namespace")
class RootElement {
to create xml file from java, it creates the root element as:
<ns2:RootElement xmlns:ns2="namespace">
but I wanted to create without the “ns2”, like:
<RootElement xmlns="namespace">
Any idea how to fix it?
Reletad link (example I used to create the xml):
http://www.java2s.com/Code/JavaAPI/javax.xml.bind.annotation/XmlRootElementname.htm
JAXB doesn’t use
xmlns = "namespace"in your case becausexmlns = "namespace"also specifies namespace for the child elements, then yourfirstandlastelements are in default namespace (because@XmlRootElementdoesn’t specify namespace for the child elements). So, you need to set namespace forfirstandlastusing@XmlElement:You can also avoid the need to write namespace for every element by using package-level annotation in
package-info.java: