KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am using JAXP to generate and parse an XML document from which some fields are loaded from a database. Code to serialize the XML: DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.newDocument(); Element root = doc.createElement("test"); root.setAttribute("version", text); doc.appendChild(root); DOMSource domSource = new DOMSource(doc); TransformerFactory tFactory = TransformerFactory.newInstance(); FileWriter out = new FileWriter("test.xml"); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(domSource, new StreamResult(out)); Code to parse the XML: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("test.xml"); And I encounter the following exception: [Fatal Error] test.xml:1:4: Invalid byte 1 of 1-byte UTF-8 sequence. Exception in thread "main" org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence. at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) at com.test.Test.xml(Test.java:27) at com.test.Test.main(Test.java:55) The String text includes u-umlaut and o-umlaut (character codes 0xFC and 0xF6). These are the characters that are causing the error. When I escape the String myself to use ü and ö then the problem goes away. Other entities are automatically encoded when I write out the XML. How do I get my output to be written / read properly without substituting these characters myself? (I've read the following
Tags (comma-separated)
Save Edits
Cancel