找到你要的答案

Q:Set xml encoding

Q:设置XML编码

I am sending xml to a web service and there I am converting input xml to string and now I am having a problem setting its encoding. Here is a code:

        Element soapinElement = (Element) streams.getSoapin().getValue().getAny();          
        Node node = (Node) soapinElement;
        Document document = node.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();         
        LSSerializer serializer = domImplLS.createLSSerializer();
        LSOutput output = domImplLS.createLSOutput();
        output.setEncoding("UTF-8");
        Writer stringWriter = new StringWriter();
        output.setCharacterStream(stringWriter);
        serializer.write(document, output);    
        String soapinString = stringWriter.toString();

This code makes a String from request xml. The problem is that when the request XML is encoded not in UTF-8 it produces unreadable characters inside xml elements:

<some element>РћР’Р” Р’РћР</some element>

When I send UTF-8 encoded xml there is no problem. So the question is how to set UTF-8 encoding when converting xml to String.

Default encoding used by JVM is ISO8859-1.

我将XML发送到一个web服务,在那里我将输入XML转换为字符串,现在我有一个问题,设置它的编码。这里是一个代码:

        Element soapinElement = (Element) streams.getSoapin().getValue().getAny();          
        Node node = (Node) soapinElement;
        Document document = node.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();         
        LSSerializer serializer = domImplLS.createLSSerializer();
        LSOutput output = domImplLS.createLSOutput();
        output.setEncoding("UTF-8");
        Writer stringWriter = new StringWriter();
        output.setCharacterStream(stringWriter);
        serializer.write(document, output);    
        String soapinString = stringWriter.toString();

此代码从请求xml中生成字符串。问题是,当请求的XML编码不是UTF-8产生不可读的字符在XML元素:

<some element>РћР’Р” Р’РћР</some element>

当我把UTF-8编码的XML是没有问题的。所以问题是如何设置UTF-8编码的XML字符串转换时。

默认的编码使用的JVM上。

answer1: 回答1:

The setEncoding method says what the encoding actually is, not what you want it to be. The XML library won't convert the characters.

See this question: Meaning of XML encoding

If you want to convert the encoding, that is another question.

本法所说的,编码实际上是,不是你想的那样。xml库不会转换字符。

看到这个问题:XML编码的含义

如果你想转换编码,那是另一个问题。

answer2: 回答2:

I would rethink my whole approach if I were you, generally XML should be kept as a tree.

But if you really need a string, try this

    final StringWriter sw = new StringWriter();
    try {
        TransformerFactory.newInstance().newTransformer().transform(
                new DOMSource(document),
                new StreamResult(sw)
        );
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }

    // Now you have the XML as a String:
    System.out.println(sw.toString());

如果我是你的话,我会重新考虑我的整个方法,一般来说XML应该被保存为一棵树。

但是如果你真的需要一个字符串,试试这个

    final StringWriter sw = new StringWriter();
    try {
        TransformerFactory.newInstance().newTransformer().transform(
                new DOMSource(document),
                new StreamResult(sw)
        );
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }

    // Now you have the XML as a String:
    System.out.println(sw.toString());
java  xml