本文介绍了在Spring MVC中使用dom4j在xml元素中编码不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个AbstractView,以便将一些XML输出到浏览器,如下所示:

I've created an AbstractView in order to output some XML to the browser, as follows:

public abstract class AbstractXmlView extends AbstractView {

public AbstractXmlView() {
    setContentType("application/xml");
}

@Override
protected void renderMergedOutputModel(Map<String, Object> model,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    response.setCharacterEncoding("UTF-8");

    Document document = new DOMDocument();
    document.setXMLEncoding("UTF-8");

    buildXmlDocument(model, document, request, response);

    response.getOutputStream().print(document.asXML());
}

public abstract void buildXmlDocument(Map<String, Object> model,
        Document document, HttpServletRequest request,
        HttpServletResponse response) throws Exception;

如您所见,我的子类将定义buildXMLDocument方法,以便填充将实际交付给浏览器的XML文档,因此这是一个简化的实现:

As you can see, my subclasses would define the buildXMLDocument method in order to populate the XML Document that would be actually delivered to the browser, so here's a simplified implementation:

public class GetXmlContacts extends AbstractXmlView {

@Override
public void buildXmlDocument(Map<String, Object> model, Document document,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    Element node = document.addElement("node");
    node.setText ("I'm unicode áéíóú");
}

请注意元素node我是Unicodeáéíóú"中的文字字符串为文本.当我向服务器请求此请求时,我获得了带有UTF-8编码(OK)的HTTP响应,XML定义说它是UTF-8,但是该节点的文本将被编码为ISO-8859-1(这是我的猜测,因为当我使用Firefox更改编码时,该字符串看起来不错).

Please note the literal string as text in the element node "I'm unicode áéíóú". When I request this to the server, I obtain an HTTP response with UTF-8 encoding (OK), the XML definition says it's UTF-8, but the node's text would be encoded as ISO-8859-1 (this is my guess, because when I change the encoding with Firefox that string looks OK).

那么,为什么dom4j在定义应为UTF-8的情况下将文字字符串编码为ISO?我的代码有问题吗?谢谢

So, why is dom4j enconding a literal string as ISO when it's defined that should be UTF-8? Is there something wrong with my code? Thanks

推荐答案

已解决!由于dom4j的某些错误,element.setText()不会关心指定的编码,而document.asXML()会返回ISO字符串,因此我对该行进行了如下修改:

Solved!Because some bug with dom4j, element.setText() wouldn't care of the specified encoding and document.asXML() would return an ISO string, so I modified that line as follows:

response.getOutputStream().write(document.asXML().getBytes("UTF-8"));

一切正常..

这篇关于在Spring MVC中使用dom4j在xml元素中编码不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 23:44