Skip to main content

updateXML issue?

No replies
krraghavan
Offline
Joined: 2007-03-02

I posted this in other forums but didn't get a response. I'm really hoping that someone here can help. In the test program below, if I unmarshal and marshal the file before calling updateXML, the comments are correctly saved. But the moment I call updateXML the comments get nuked.
---------------------------------

I'm trying to use the Binder class and updateXML method to retain the comments. I'm just unable to get this to work. Here's my test program:

JAXBContext ctx = JAXBContext.newInstance("com.foo");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
File f = new File("D:/bar.xml");
factory.setIgnoringComments(false);
factory.setNamespaceAware(true);
DocumentBuilder bldr = factory.newDocumentBuilder();

Document doc = bldr.parse(f);
OutputFormat fmt = new OutputFormat();
fmt.setIndenting(true);
fmt.setOmitComments(false);
fmt.setOmitXMLDeclaration(false);

Binder binder = ctx.createBinder();
binder.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
binder.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "control.xsd");

// Create JAXB object
ConfigurationModule module = (ConfigurationModule) binder.unmarshal(doc);

// add a new object to the JAXB representation
CliCmd cmd = new CliCmd();
cmd.setId(String.format("%s_%s", "TESTING", Long.toString(System.currentTimeMillis())));
module.getTypedefOrEnumOrValueSet().add(cmd);

// write to a new file - expect to see comments + new node in this file.
File f1 = new File("D:/foo1.xml");
Element d1e = (Element)binder.updateXML(module);
XMLSerializer ser = new XMLSerializer(new FileOutputStream(f1), fmt);
ser.serialize(d1e.getOwnerDocument());

-----------
File foo1.xml does not contain comments. I've tried everything I can think of - can anyone help.

Raghavan