1

我正在尝试解析嵌入在文件中的 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。此外,这是我在堆栈跟踪顶部得到的异常:

12-18 13:51:12.978: W/System.err(5784): org.xml.sax.SAXParseException: 预期名称(位置:START_TAG @1:2 in java.io.InputStreamReader@4118c880)12-18 13: 51:12.978: W/System.err(5784): 在 org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146) 12-18 13:51:12.978: W/System.err(5784 ): 在 javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107)

4

2 回答 2

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);

资料来源:如何从字符串创建 InputStream?

于 2012-12-17T19:18:36.840 回答
1

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

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

Document dom = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
于 2012-12-17T22:37:25.627 回答