变压器中允许的特殊性

时间:2014-12-16 10:27:45

标签: java xml encoding utf-8 character-encoding

我正在尝试将xml文件发送到服务器后处理它有一些特殊字符,如“佥佬佧佼A£”。

目前代码看起来像

 public class Utils {
 public static String transformToString(Document activeDocument) throws TransformerException{
 TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "true");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(writer));
    writer.toString();

}

测试课

public class Test {
     public static void main(String[] args){
         Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlPath);
         //Doing some process on doc and changing other values

         String xmlString = Utils.transformToString(doc);

         // Sending xml to soap
            HttpPost post = new HttpPost(endpoint);
    post.setEntity(new StringEntity(xmlString));
    post.setHeader(new BasicHeader("content-type", "text/xml"));
               .................
     } 
}

XML文件

 <?xml version="1.0" encoding="UTF-8"?>
  <soapenv:Envelope ....>
<soapenv:Header/>
<soapenv:Body>
<Test>
    <Id>佥佬佧佼A£</Id>
    ..............
    ..........
</Test>
.........

我得到带有Id值的xmlString:???? A£ 我想要的是:佥佬佧佼A£

我该怎么做?我只想要那个XML的String格式。

编辑:我正在加载一个XML,对其进行一些更改,并将该XML发送到SOAP,方法是将该文档设置为httpPost.setEntity()

谢谢, ANKIT

1 个答案:

答案 0 :(得分:0)

您可以尝试:

encoding="UTF-16"

而不是

encoding="UTF-8"