Hallo,
folgende Situation: ich füge ein w3c.DOM-Element (mit Namespace ohne Prefix) in ein mit @XmlAnyElement annotiertes Objekt ein und machen dann ein Marshalling. Leider wird dabei automatisch ein Prefix angehängt + ein zusätzlicher Namespace generiert. Ich habe mal ein einfaches Beispielprogramm geschrieben was den Effekt zeigt.
Also mein Ziel ist:
[XML]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>
<desciption>
<dom xmlns="www.google.de"/>
</desciption>
</test>
[/XML]
Hat jemand eine Idee wie ich das erreichen kann?
folgende Situation: ich füge ein w3c.DOM-Element (mit Namespace ohne Prefix) in ein mit @XmlAnyElement annotiertes Objekt ein und machen dann ein Marshalling. Leider wird dabei automatisch ein Prefix angehängt + ein zusätzlicher Namespace generiert. Ich habe mal ein einfaches Beispielprogramm geschrieben was den Effekt zeigt.
Java:
public class JAXBNamespaceTest {
private static JAXBContext jaxbContext;
private static Marshaller marshaller;
static {
try {
jaxbContext = JAXBContext.newInstance(TestJAXB.class);
marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
} catch (Exception exc) {
exc.printStackTrace();
}
}
public void nsTest() throws Exception {
// create dom
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = builder.newDocument();
org.w3c.dom.Element e = doc.createElement("dom");
e.setAttribute("xmlns", "www.google.de");
doc.appendChild(e);
// print dom
System.out.println("So möchte ich das haben, ein Element ohne Prefix mit Namespace Attribut");
xmlOut(doc); // perfect - only one xmlns tag
System.out.println();
System.out.println();
// create jaxb stuff
TestJAXB testJAXB = new TestJAXB();
testJAXB.desciption.any = e;
System.out.println("Und hier mit Prefix 'dom' und extra Namespace 'xmlns:dom'");
marshaller.marshal(testJAXB, System.out); // why dom:dom and xmlns:dom?
}
@XmlRootElement(name="test")
public static class TestJAXB {
public DescriptionType desciption = new DescriptionType();
}
@XmlType(name = "descriptionType")
public static class DescriptionType {
@XmlAnyElement(lax = false)
public Object any;
}
public static void xmlOut(org.w3c.dom.Document doc) throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Source source = new DOMSource(doc);
Result output = new StreamResult(System.out);
transformer.transform(source, output);
}
public static void main(String[] args) throws Exception {
JAXBNamespaceTest nsTest = new JAXBNamespaceTest();
nsTest.nsTest();
}
}
Also mein Ziel ist:
[XML]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>
<desciption>
<dom xmlns="www.google.de"/>
</desciption>
</test>
[/XML]
Hat jemand eine Idee wie ich das erreichen kann?