tags:

views:

60

answers:

2

I'm parsing a ServletRequest object(well, a HttpServletRequest really) Have 2 parameters there, one of them I know to have a value which is an XML file.

How can I retrieve that XML as Doc or byte[] or similar, rather than String?

+1  A: 
  • You can convert String to byte[] easily enough using its getBytes() function.

  • If you want a Doc, you'll have to parse it. To do that, you can get a DocumentBuilder from DocumentBuilderFactory and let that parse() a ByteArrayOutputStream wrapped around that byte array.

Carl Smotricz
Thanks a lot! The suggested worked perfectly!
akapulko2020
+2  A: 

If you have a string which contains XML, you can parse that into a Document by parsing from a StringReader which wraps the String. Don't convert the String to bytes unless you can deal with the potential encoding issues.

builder.parse(new InputSource(new StringReader(theString))

Paul Clapham
More strongly, don't convert the String to bytes so the parser has to convert from bytes back to a character stream. That's two unnecessary transcoding steps that may not agree on the character set and subtly corrupt the data.
Alan Krueger
Thanks, I'll try to refactor into your solution as soon as possible
akapulko2020