rplankenhorn
rplankenhorn

Reputation: 2084

Java Parsing XML from UTF-16LE string

I am trying to parse a UTF-16LE XML string that is embedded within a file. 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. 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.

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. Also, here is the exception I get at the top of the 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)

Upvotes: 1

Views: 2817

Answers (2)

rplankenhorn
rplankenhorn

Reputation: 2084

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?

Upvotes: 2

Raffaele
Raffaele

Reputation: 20875

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

Upvotes: 1

Related Questions