简体   繁体   English

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

[英]Java Parsing XML from UTF-16LE string

I am trying to parse a UTF-16LE XML string that is embedded within a file. 我试图解析嵌入在文件中的UTF-16LE XML字符串。 I am able to read the actual string into a String object and I can view the XML in the watch window and it looks fine. 我能够将实际的字符串读入String对象,我可以在监视窗口中查看XML,看起来很好。 The problem is that when I try and parse it, an exception keeps getting thrown. 问题是,当我尝试解析它时,异常会不断被抛出。 I have tried to specify UTF-16 and UTF-16LE in the getBytes line and in the InputStreamReader constructor but it still throws the exception. 我试图在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

Edit: This is using Android. 编辑:这是使用Android。 Also, here is the exception I get at the top of the STACK TRACE: 此外,这是我在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) 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) ):在javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107)

Here is what I ended up doing: 这是我最终做的事情:

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

Source: How does one create an InputStream from a String? 来源: 如何从String创建InputStream?

There's no need to convert back and forth between strings and byte in the same program. 不需要在同一程序中在字符串和字节之间来回转换。 It's just as easy as: 这很简单:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM