Java从UTF-16LE字符串解析XML

时间:2012-12-17 17:19:36

标签: java android xml saxparser utf-16

我正在尝试解析嵌入在文件中的UTF-16LE XML字符串。我能够将实际的字符串读入String对象,我可以在监视窗口中查看XML,看起来很好。问题是,当我尝试解析它时,异常会不断被抛出。我试图在getBytes行和InputStreamReader构造函数中指定UTF-16和UTF-16LE,但它仍然抛出异常。

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;

builder = builderFactory.newDocumentBuilder();      
Document document = null;
byte[] bytes = xmlString.getBytes();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
InputSource is = new InputSource(new InputStreamReader(inputStream));
document = builder.parse(is); // throws SAXParseException

编辑:这是使用Android。此外,这是我在STACK TRACE顶部得到的例外:

12-18 13:51:12.978:W / System.err(5784):org.xml.sax.SAXParseException:name expected(position:START_TAG @ 1:2 in java.io.InputStreamReader@4118c880) 12-18 13:51:12.978:W / System.err(5784):at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146) 12-18 13:51:12.978:W / System.err(5784):at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107)

2 个答案:

答案 0 :(得分:2)

这是我最终做的事情:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;

builder = builderFactory.newDocumentBuilder();      
Document document = null;
byte[] bytes = Charset.forName("UTF-16LE").encode(xmlString).array();
InputStream inputStream = new ByteArrayInputStream(bytes);
document = builder.parse(inputStream);

来源:How does one create an InputStream from a String?

答案 1 :(得分:1)

不需要在同一程序中的字符串和字节之间来回转换。这很简单:

String xml = "<root><tag>Hello World!</tag></root>";

Document dom = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder().parse(new InputSource(new StringReader(xml)));