如何在Java中将xsl应用于xml

use*_*508 8 java xml xslt

这是被问过很多次的问题,但我并没有真正找到我正在寻找的东西.我通常不用Java编写代码,而是用C#编写代码,所以我对Java类等不熟悉.

我需要创建一个需要2个参数的方法.1.一个字符串参数(xml - 所以需要转换为某个xml类)2.带有xsl文件路径位置的字符串参数

问题是我正在制作一个工厂类,必须将xml从webservice转换为我的系统可以理解的xml.我需要一个很好的解决方案.ws上的每个方法都有一个xsl文件 - 请求(将我的xml转换为ws理解的东西)和响应(转换为我的系统理解的东西).

Tho*_*sen 17

您可能会发现Java Almanac是一个有用的资源.

特别是使用XSL转换XML文件的典型程序.从页面复制的示例(因为它一直在消失)

import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class BasicXsl {
    // This method applies the xslFilename to inFilename and writes
    // the output to outFilename.
    public static void xsl(String inFilename, String outFilename, String xslFilename) {
        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(xslFilename)));

            // Use the template to create a transformer
            Transformer xformer = template.newTransformer();

            // Prepare the input and output files
            Source source = new StreamSource(new FileInputStream(inFilename));
            Result result = new StreamResult(new FileOutputStream(outFilename));

            // Apply the xsl file to the source file and write the result
            // to the output file
            xformer.transform(source, result);
        } catch (FileNotFoundException e) {
        } catch (TransformerConfigurationException e) {
            // An error occurred in the XSL file
        } catch (TransformerException e) {
            // An error occurred while applying the XSL file
            // Get location of error in input file
            SourceLocator locator = e.getLocator();
            int col = locator.getColumnNumber();
            int line = locator.getLineNumber();
            String publicId = locator.getPublicId();
            String systemId = locator.getSystemId();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

样本输入:

<?xml version="1.0" encoding="UTF-8"?>
<map>
    <entry key="key1" value="value1" />
    <entry key="key2" />
</map>
Run Code Online (Sandbox Code Playgroud)

示例XSLT程序:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" indent="yes"/>

<xsl:template match="map">
<HTML>
<HEAD>
<TITLE>Map</TITLE>
</HEAD>
<BODY>
    <xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>


<xsl:template match="entry">
    <xsl:value-of select="@key"/>=<xsl:value-of select="@value"/>
    <br></br>
</xsl:template>


</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

运行该示例生成的HTML是:

<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Map</TITLE>
</HEAD>
<BODY>
    key1=value1<br>
    key2=<br>

</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)