javaxmlxom

How to set an xml declaration in XOM use Java?


Element element = new Element("root");
Document document = new Document(element);

document will auto generate <?xml version="1.0"?>

<?xml version="1.0"?>
<root>
</root>

How to set encoding, standalone into xml declaration use XOM ? I expect:

<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<root>
</root>

Solution

  • Short answer: you can't do that.

    XOM deliberately does not expose or emit the standalone declaration. In 18 years of XML, I can't recall a single case where the standalone declaration has been necessary or useful; and XOM makes a point of shielding developers from features they shouldn't concern themselves with.

    The standalone declaration likely doesn't do what you think it does in any case. For instance, both of the highest voted answers on What does the 'standalone' directive mean in XML? are wrong. I'll add a correct answer to that question when I get a minute.