Search code examples
javaxmlparsingvtd-xml

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


Solution

  • 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).