0

I'm splitting an incoming xml file using JAXB. I know this seems like a duplicate thread, However

Expected,

<?xml version="1.0"?>

Actual,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

I tried using JAXB_FRAGMENT which completely removes the first line

jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

And

jaxbMarshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

with and without Marshaller.JAXB_FRAGMENT

FYI, I'm using StringWriter

        JAXBContext jaxbContext = JAXBContext.newInstance(ExportDocument.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);//

        //jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        //jaxbMarshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");

        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(employee, sw);
        String xmlContent = sw.toString();

        System.out.println(xmlContent);
vbnr
  • 307
  • 2
  • 5
  • 14
  • Is the emitted document in fact *not* effectively standalone? Or else, why is this an issue? – John Bollinger May 31 '19 at 12:55
  • No, I don't want to enforce validity constraints with external DTD. Incoming XML has no encoding and standalone fields. So the spit xmls need not have them. – vbnr May 31 '19 at 13:06
  • 1
    Ok, but (1) that's not the effect of `standalone="no"` (the default), and (2) unless there's a `DOCTYPE` declaration, there is no external DTD to validate against anyway. In fact, `standalone="yes"` seems closer to what you want (though it doesn't disable validation against an external DTD, either). – John Bollinger May 31 '19 at 13:09
  • I see,what about the encoding part? Does it effect in anyway? – vbnr May 31 '19 at 13:11
  • 1
    The encoding part specifies the character encoding of the document. You could safely omit it in this case because UTF-8 is the default anyway, but it is not harmful. It is unlikely to be worth expending effort on suppressing that. – John Bollinger May 31 '19 at 13:13
  • Thanks for clarifying, however incoming xml has only version, not having them in split xmls makes more sense. Still wanna know how I can remove them. – vbnr May 31 '19 at 13:19
  • 1
    I recognize that my comments do not answer the question posed. That's why they're comments, not an answer. But you should take them as a frame challenge: instead of trying to solve the problem of getting rid of (at worst) harmless attributes, consider solving the problem of making needless work for yourself. – John Bollinger May 31 '19 at 13:23

1 Answers1

0

I was actually looking for the exact same thing and this is what I found Altering the XML header produced by the JAXB marshaller

Prasad
  • 368
  • 1
  • 6
  • 16