How to encode characters from Oracle to XML?

前端 未结 3 1608
甜味超标
甜味超标 2021-01-12 05:28

In my environment here I use Java to serialize the result set to XML. It happens basically like this:

//foreach column of each row
xmlHandler.startElement(ur         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 05:55

    I found an interesting list in the Xml Spec: According to that List its discouraged to use the Character #26 (Hex: #x1A).

    The characters defined in the following ranges are also discouraged. They are either control characters or permanently undefined Unicode characters

    See the complete ranges.

    This code replaces all non-valid Xml Utf8 from a String:

    public String stripNonValidXMLCharacters(String in) {
        StringBuffer out = new StringBuffer(); // Used to hold the output.
        char current; // Used to reference the current character.
    
        if (in == null || ("".equals(in))) return ""; // vacancy test.
        for (int i = 0; i < in.length(); i++) {
            current = in.charAt(i);
            if ((current == 0x9) ||
                (current == 0xA) ||
                (current == 0xD) ||
                ((current >= 0x20) && (current <= 0xD7FF)) ||
                ((current >= 0xE000) && (current <= 0xFFFD)) ||
                ((current >= 0x10000) && (current <= 0x10FFFF)))
                out.append(current);
        }
        return out.toString();
    }    
    

    its taken from Invalid XML Characters: when valid UTF8 does not mean valid XML

    But with that I had the still UTF-8 compatility issue:

    org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence
    

    After reading XML - returning XML as UTF-8 from a servlet I just tried out what happens if I set the Contenttype like this:

    response.setContentType("text/xml;charset=utf-8");
    

    And it worked ....

提交回复
热议问题