0

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

Paul
  • 547
  • 4
  • 13
  • 30
  • you're comparing an String against a closed XMLByteOutputStream? variable names could be a bit more telling too. – Curiosa Globunznik Feb 06 '20 at 09:06
  • @güriösä : Thanks, should have been comaped against toString(). I edited this. But key is that my original XML is modified without any modifications performed – Paul Feb 06 '20 at 09:39

1 Answers1

2

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.

Daniil
  • 913
  • 8
  • 19