当前位置: 技术问答>java相关
用tomcat在服务端用xslt解析xml生成html的中文问题!
来源: 互联网 发布时间:2015-05-05
本文导语: 用tomcat在服务端用xslt解析xml生成html的中文问题! 用如下代码可以正确解析成html但怎样出来里面的中文问题阿! 在那儿设置编码? Document document; DocumentBuilderFactory factory = DocumentBuilder...
用tomcat在服务端用xslt解析xml生成html的中文问题!
用如下代码可以正确解析成html但怎样出来里面的中文问题阿!
在那儿设置编码?
Document document;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse("http://wind/egold/template/articleClass.xml");
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
StreamSource stylesource = new StreamSource("http://wind/egold/xsl/articleClass.xsl");
Transformer transformer = tFactory.newTransformer(stylesource);
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
用如下代码可以正确解析成html但怎样出来里面的中文问题阿!
在那儿设置编码?
Document document;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse("http://wind/egold/template/articleClass.xml");
// Use a Transformer for output
TransformerFactory tFactory =
TransformerFactory.newInstance();
StreamSource stylesource = new StreamSource("http://wind/egold/xsl/articleClass.xsl");
Transformer transformer = tFactory.newTransformer(stylesource);
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
|
怎么贴出来总有问题,呵呵。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
/**
* The simplest possible servlet.
*
* @author James Duncan Davidson
*/
public class ABC extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
try {
OutputStream out=response.getOutputStream();
String xml = ""
+ "n"
+ "n"
+ "n "
+ "n 中文中文中文中文中文中文 中文中文."
+ "n 中文中文 中文中文."
+ "n Little中文中文Tinamou."
+ "n 中文中文 Tinamou."
+ "n Slaty-中文中文 Tinamou."
+ "n Choco 中文中文."
+ "n "
+ "n "
+ "n"
+ "n";
String xsl = " "
+ ""
+ " "
+ ""
+ ""
+ " "
+ ""
+ ""
+ ""
+ "Order is中文中文: "
+ " "
+ ""
+ ""
+ ""
+ " 中文中文 is: "
+ " "
+ ""
+ ""
+ " 中文中文 is "
+ " "
+ ""
+ ""
+ " "
+ " "
+ " "
+ " "
+ " "
+ " "
+ " "
+ " "
+ ""
+ "";
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(new StreamSource(new StringReader(xsl)));
t.transform(new StreamSource(new StringReader(xml)), new StreamResult(out));
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
/**
* The simplest possible servlet.
*
* @author James Duncan Davidson
*/
public class ABC extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
try {
OutputStream out=response.getOutputStream();
String xml = ""
+ "n"
+ "n"
+ "n "
+ "n 中文中文中文中文中文中文 中文中文."
+ "n 中文中文 中文中文."
+ "n Little中文中文Tinamou."
+ "n 中文中文 Tinamou."
+ "n Slaty-中文中文 Tinamou."
+ "n Choco 中文中文."
+ "n "
+ "n "
+ "n"
+ "n";
String xsl = " "
+ ""
+ " "
+ ""
+ ""
+ " "
+ ""
+ ""
+ ""
+ "Order is中文中文: "
+ " "
+ ""
+ ""
+ ""
+ " 中文中文 is: "
+ " "
+ ""
+ ""
+ " 中文中文 is "
+ " "
+ ""
+ ""
+ " "
+ " "
+ " "
+ " "
+ " "
+ " "
+ " "
+ " "
+ ""
+ "";
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(new StreamSource(new StringReader(xsl)));
t.transform(new StreamSource(new StringReader(xml)), new StreamResult(out));
} catch (Exception e) {
e.printStackTrace();
}
}
}