我有以下xml:
<?xml version="1.0" encoding="UTF-8"?>
<roots xmlns="http://www.test.com/test/rest/v1">
<root>
<name>james</name>
</root>
</roots>
我想为上述xml生成JAXB类,并在封送处理时动态添加名称空间(xmlns="http://www.test.com/test/rest/v1"
)。我可以生成JAXB类,但不能动态添加名称空间。我尝试使用以下代码,但无法正常工作。关于如何做到这一点的任何想法?
JAXBContext jaxbContext = JAXBContext.newInstance("com.test");
XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(inputSchema);
xmlStreamWriter.setPrefix("xmlns", "http://www.test.com/test/rest/v1");
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.marshal(roots, xmlStreamWriter);