How can JAXB handle a string (of XML) that starts with a BOM? Is there a property to set or some configuration to skip the BOM when unmarshalling?Are there other JAXB implementations that could do that beside Oracle's implementation?
Asked
Active
Viewed 2,069 times
1 Answers
3
If you're referring to a UTF-8 file with a BOM, then you will have to skip it yourself. It's fairly simple to come up with an InputStream class that checks the first two bytes for a BOM and skips them and otherwise wraps another InputStream. This has been documented in this SO answer and open source code for this purpose is available from GitHub.
If you're referring to some other encoding like UTF-16, the JRE should read the BOM from a UTF-16 stream and discard it itself.
-
I have a string that is an xml content , something like this ' ....' and I create a StringReader to read the content into unmarshaller like this: unmarshaller.unmarshal(new StringReader(xml)); but the string start with a BOM and here came the problem since the unmarshalling process crash, I thought JAXB has some property to set up and handle by it self the BOM char without any changes from me. – srncristea Jan 26 '15 at 15:51
-
2No, you still have to do it yourself, sadly. May I also call your attention to the [Apache Commons IO](http://commons.apache.org/proper/commons-io/) project, especially the [BOMInputStream](http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/input/BOMInputStream.html) and/or the [XmlStreamReader](http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/input/XmlStreamReader.html) classes? – dcsohl Jan 26 '15 at 16:39
-
yes, I'll have to take a look into that classes, thanks a lot for your help ;). – srncristea Jan 26 '15 at 21:09