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);
3
  • Look into this link. stackoverflow.com/questions/16979762/… Commented May 13, 2019 at 15:56
  • Hope it answers your question. Commented May 13, 2019 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? Commented May 13, 2019 at 16:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.