1

i am writing a xml, using JDOM2

the customers wish is, to have a line as follows:

<VT xml:space="preserve">&#50;&#48;</VT>

but all I can create is:

<VT xml:space="preserve">20</VT>

because the ascii is converted by my format encoding ("ISO-8859-1")

how to prevent this element from being converted...?

this is, what i use:

String str = "&#50;";
String unescapeXml = StringEscapeUtils.unescapeXml(str);
element.addContent(unescapeXml);

also i use:

format = Format.getPrettyFormat();
format.setEncoding("ISO-8859-1");
format.setExpandEmptyElements(true);
XMLOutputter out;
OutputStreamWriter fw = null;

out = new XMLOutputter();
out.setFormat(format);

try {
    fw = new OutputStreamWriter(new FileOutputStream(file));
    PrintStream printStream = System.out;

    // fw = new FileWriter(file);

    out.output(doc, printStream);
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
unknown404
  • 115
  • 9
  • The XML specification indicates that the two formats are identical. There is no difference between them. it is like the difference between 1000mm and 1 meter - except in this case the convention is to use the `2` format instead of `2`. If your customer is insisting on the `2` type format then their systems are not using standard-compliant XML parsers (you should recommend that they use JDOM ;-) ) – rolfl Jan 18 '17 at 12:35
  • @rolfl thank you, this was also, what I was thinking. – unknown404 Jan 18 '17 at 12:41
  • but still, is there any possibillity in jdom-2 to print out the ascii-format? – unknown404 Jan 18 '17 at 12:41

1 Answers1

0

In JDOM you can establish rules for telling the output formatter when to escape character output. It is highly unconventional, but you could probably build an Escapetrategy like:

private static final EscapeStrategy alldigits = new EscapeStrategy() {
    @Override
    public boolean shouldEscape(char ch) {
        return Character.isDigit(ch) || DefaultEscapeStrategy.shouldEscape(ch);
    }
};

The above instance will cause all digits, and any other regularly-escaped characters to be escaped.

You can then set that strategy on an instance of your output formatter:

format = Format.getPrettyFormat();
format.setEncoding("ISO-8859-1");
format.setExpandEmptyElements(true);
format.setEscapeStrategy(alldigits);

Read up more about the EscapeStrategy here: http://jdom.org/docs/apidocs/org/jdom2/output/EscapeStrategy.html

and the way it is used in JDOM here: https://github.com/hunterhacker/jdom/blob/master/core/src/java/org/jdom2/output/Format.java#L147 and here: https://github.com/hunterhacker/jdom/blob/master/core/src/java/org/jdom2/output/support/AbstractXMLOutputProcessor.java#L765

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • thanks again, this causes a lot of trouble throughout the whole xml. also, this converts a 2 into 2 and not my desired 2 so this would cause the troubles, to let this happen only locally, delete the x and add 18... :D – unknown404 Jan 18 '17 at 14:01