首先写了几个转换函数: public static boolean saveNodeToStream(Node node, OutputStream out){ try {
Source source = new DOMSource(node); Result result = new StreamResult(out);
Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (Exception e){
return false;
}
return true;
} public static String saveNodeToString(Node node){
ByteArrayOutputStream bout=new ByteArrayOutputStream();
if(!saveNodeToStream(node,bout)){
Utils.CloseStream(bout);
return null;
}
try{
bout.flush();
String r=bout.toString();
bout.close();
return r;
}catch(IOException e){
return null;
} } public static Document transform(String xml_filename, String xsl_filename) {
try {
// Create transformer factory
TransformerFactory factory = TransformerFactory.newInstance(); // Use the factory to create a template containing the xsl file
Templates template = factory.newTemplates(new StreamSource(
new FileInputStream(xsl_filename))); // Use the template to create a transformer
Transformer xformer = template.newTransformer(); // Prepare the input file
Source source = new StreamSource(new FileInputStream(xml_filename)); // Create a new document to hold the results
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Result result = new DOMResult(doc);
// Apply the xsl file to the source file and create the DOM tree
xformer.transform(source, result);
return doc;
} catch (ParserConfigurationException e) {
// An error occurred while creating an empty DOM document
} catch (FileNotFoundException e) {
} catch (TransformerConfigurationException e) {
// An error occurred in the XSL file
} catch (TransformerException e) {
// An error occurred while applying the XSL file
}
return null;
}
然后用以下代码测试:
public static void main(String[] args) {
String xml="c:/test/1.xml";
String xsl="c:/test/pi.xsl";
Document doc=XMLUtils.transform(xml,xsl);
if(doc==null)return;
System.out.print(XMLUtils.saveNodeToString(doc));
}其中,1.xml如下:<?xml version='1.0' encoding='gb2312' ?>
<customers>
<customer>
<name>我</name>
<address>123 Elm St.</address>
<phone>(123) 456-7890</phone>
</customer>
<customer>
<name>Amy Jones</name>
<address>456 Oak Ave.</address>
<phone>(156) 789-0123</phone>
</customer>
</customers>pi.xsl如下:<?xml version='1.0' encoding="gb2312"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:output method='xml' version='1.0' encoding="gb2312"/> <<===========注意这里是gb2312
<xsl:template match="/"> <xsl:apply-templates />
</xsl:template> <xsl:template match="@* | *">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template></xsl:stylesheet>//////////////////////而产生的结果如下:<?xml version="1.0" encoding="UTF-8"?> <<=========注意这里是utf-8而不是gb2312<customers><customer> <name>鎴?/name> <<==========注意这里是"乱码" <address>123 Elm St.</address> <phone>(123) 456-7890</phone></customer><customer> <name>Amy Jones</name> <address>456 Oak Ave.</address> <phone>(156) 789-0123</phone></customer></customers>////////////////////////////////为什么在xsl:output 的encoding里面指定了编码方式,生成的xml文件编码方式却变成了utf-8 ?????????????????????????困惑不已!!!!!!!!!!!!!1
Source source = new DOMSource(node); Result result = new StreamResult(out);
Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (Exception e){
return false;
}
return true;
} public static String saveNodeToString(Node node){
ByteArrayOutputStream bout=new ByteArrayOutputStream();
if(!saveNodeToStream(node,bout)){
Utils.CloseStream(bout);
return null;
}
try{
bout.flush();
String r=bout.toString();
bout.close();
return r;
}catch(IOException e){
return null;
} } public static Document transform(String xml_filename, String xsl_filename) {
try {
// Create transformer factory
TransformerFactory factory = TransformerFactory.newInstance(); // Use the factory to create a template containing the xsl file
Templates template = factory.newTemplates(new StreamSource(
new FileInputStream(xsl_filename))); // Use the template to create a transformer
Transformer xformer = template.newTransformer(); // Prepare the input file
Source source = new StreamSource(new FileInputStream(xml_filename)); // Create a new document to hold the results
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Result result = new DOMResult(doc);
// Apply the xsl file to the source file and create the DOM tree
xformer.transform(source, result);
return doc;
} catch (ParserConfigurationException e) {
// An error occurred while creating an empty DOM document
} catch (FileNotFoundException e) {
} catch (TransformerConfigurationException e) {
// An error occurred in the XSL file
} catch (TransformerException e) {
// An error occurred while applying the XSL file
}
return null;
}
然后用以下代码测试:
public static void main(String[] args) {
String xml="c:/test/1.xml";
String xsl="c:/test/pi.xsl";
Document doc=XMLUtils.transform(xml,xsl);
if(doc==null)return;
System.out.print(XMLUtils.saveNodeToString(doc));
}其中,1.xml如下:<?xml version='1.0' encoding='gb2312' ?>
<customers>
<customer>
<name>我</name>
<address>123 Elm St.</address>
<phone>(123) 456-7890</phone>
</customer>
<customer>
<name>Amy Jones</name>
<address>456 Oak Ave.</address>
<phone>(156) 789-0123</phone>
</customer>
</customers>pi.xsl如下:<?xml version='1.0' encoding="gb2312"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:output method='xml' version='1.0' encoding="gb2312"/> <<===========注意这里是gb2312
<xsl:template match="/"> <xsl:apply-templates />
</xsl:template> <xsl:template match="@* | *">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template></xsl:stylesheet>//////////////////////而产生的结果如下:<?xml version="1.0" encoding="UTF-8"?> <<=========注意这里是utf-8而不是gb2312<customers><customer> <name>鎴?/name> <<==========注意这里是"乱码" <address>123 Elm St.</address> <phone>(123) 456-7890</phone></customer><customer> <name>Amy Jones</name> <address>456 Oak Ave.</address> <phone>(156) 789-0123</phone></customer></customers>////////////////////////////////为什么在xsl:output 的encoding里面指定了编码方式,生成的xml文件编码方式却变成了utf-8 ?????????????????????????困惑不已!!!!!!!!!!!!!1
这句没有转码,你按gbk读入之后再试试。