0

I have the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<roots xmlns="http://www.test.com/test/rest/v1">
   <root>
      <name>james</name>
   </root>
</roots>

I want to generate the JAXB classes for the above xml and add the name space (xmlns="http://www.test.com/test/rest/v1") dynamically while marshaling. I could able to generate the JAXB classes, but not able to add the namespace dynamically. I tried with the following code, but it's not working. Any idea on how to do that?

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);
Ashok.N
  • 1,327
  • 9
  • 29
  • 64
  • Look into this link. https://stackoverflow.com/questions/16979762/adding-namespaces-to-root-element-of-xml-using-jaxb – Sambit May 13 '19 at 15:56
  • Hope it answers your question. – Sambit May 13 '19 at 15:59
  • After following the answer i am getting `xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com/test/rest/v1"` instead of just `xmlns="http://www.test.com/test/rest/v1"`. Any clue why it's happening? – Ashok.N May 13 '19 at 16:07

0 Answers0