javavtd-xml

Encoding issues with vtd-xml


I am encountering encoding issues with when using the vtd-xml library (version 2.11).

@Test
    public void test() throws Exception {
        final String originalXML = "<?xml version=\"1.0\"?>\r\n" + "<test>\r\n" + "öäüß\r\n" + "</test>\r\n" + "";
        final VTDGen vg;
        final XMLModifier xm;
        final AutoPilot ap;
        final VTDNav vn;

        vg = new VTDGen();
        // vg.setDoc(originalXML.getBytes()); --> results in
        // com.ximpleware.ParseException: UTF 8 encoding error: should never happen
        vg.setDoc(originalXML.getBytes("UTF-8"));
        vg.parse(false);

        ap = new AutoPilot();
        xm = new XMLModifier();

        vn = vg.getNav();
        ap.bind(vn);
        xm.bind(vn);

        final XMLByteOutputStream xms = new XMLByteOutputStream(xm.getUpdatedDocumentSize());
        xm.output(xms);
        xms.close();
        assertEquals(originalXML, xms.toString());
    }

This assert Statment fails with

java.lang.AssertionError: expected:<<?xml version="1.0"?>

<test>
öäüß
</test>
> but was:<<?xml version="1.0"?>
<test>
ᅢ쌔내태゚
</test>

Any idea how to fix that?

Thanks, Paul


Solution

  • I would suggest to use ByteArrayOutputStream instead of XMLByteOutputStream. In that case you could provide charset name when building up string from the outputstream:

    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        xm.output(os);
        assertEquals(originalXML, os.toString("UTF-8"));
    }
    

    In that case special characters should be preserved.