Concept
Concept

Reputation: 43

Can VTD-XML take a String as an input?

Hey, I'm trying to use VTD-XML to parse XML given to it as a String, but I can't find how to do it. Any help would be appreciated.

http://vtd-xml.sourceforge.net

Upvotes: 4

Views: 3005

Answers (2)

helios
helios

Reputation: 13841

It seems VTD-XML library lets you read byte array data. I'd suggest in that case, convert the String to bytes using the correct encoding.

If there's an encoding signaled in the begining of the XML string:

<?xml version="1.0" encoding="UTF-8"?>

Then use that:

myString.getBytes("UTF-8")

If there's not an encoding, please use one, for VTD-XML know how to decode the bytes:

String withHeader  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + myString;
byte[] bytes = withHeader.getBytes("UTF-8");
VTDGen vg = new VTDGen();
vg.setDoc(bytes);
vg.parse(true);

Note that in the later case you can use any valid encoding because the string you have in memory is encoding-agnosting (it's in UTF-16 but when you ask for the bytes it will be converted).

Upvotes: 5

vtd-xml-author
vtd-xml-author

Reputation: 3377

VTD-XML doesn't accept a string because string implies UCS-16 encoding, which means it is not really a xml document.. as defined by the spec, xml is usually encoded in utf-8, ascii, iso-8859-1 or UTF-16LE or BE format... does my answer make sense?

Upvotes: 2

Related Questions