使用XSL将Text文件转换为XML的Java程序

时间:2014-10-09 12:19:09

标签: java xml xslt

我正在尝试实现一个小例子,我希望使用XSL作为转换器将文本文件中的内容转换为XML文件。我在SO中遇到了这个例子 - XSL - create well formed xml from text file,我试图实现相同但面临一些问题。

我使用相同的文本文件作为输入和回复SO帖子中提到的XSL文件。这是我尝试使用的Java程序:

public class Parser {
    public static void main(String[] args) {
        String path="src/";
        String text = path+"input.txt";
        String xslt = path+"input.xsl";
        String output = path+"output.xml";

        System.setProperty("javax.xml.transform.TransformerFactory",    
                "net.sf.saxon.TransformerFactoryImpl");
        try {
            TransformerFactory tf = TransformerFactory.newInstance();

            Transformer tr = tf.newTransformer(new StreamSource(xslt));
            tr.transform(new StreamSource(text), new StreamResult(
                    new FileOutputStream(output)));

            System.out.println("Output to " + output);
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }
}

我得到例外:

Error on line 1 column 1 of input.txt:
  SXXP0003: Error reported by XML parser: Content is not allowed in prolog.
net.sf.saxon.trans.XPathException: org.xml.sax.SAXParseException: Content is not allowed in prolog.
net.sf.saxon.trans.XPathException: org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:418)
    at net.sf.saxon.event.Sender.send(Sender.java:214)
    at net.sf.saxon.event.Sender.send(Sender.java:50)
    at net.sf.saxon.Controller.transform(Controller.java:1611)
    at three.Parser.main(Parser.java:21)
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1427)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1036)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:404)
    ... 4 more

似乎我不能在我的程序中使用文本文件作为输入。有人可以帮我解决问题。

更新

我已经使用Saxon S9 API(使用Jar - saxon9he.jar)解决了它,正如Martin在他的回答中所建议的那样,这里有JAVA代码。

import java.io.File;

import javax.xml.transform.stream.StreamSource;

import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;
public class Parser {
    public static void main(String[] args) throws SaxonApiException {
        Processor proc = new Processor(false);
        XsltCompiler comp = proc.newXsltCompiler();
        XsltExecutable exp = comp.compile(new StreamSource(new File(
                "src/input.xsl")));
        Serializer out = new Serializer();
        out.setOutputProperty(Serializer.Property.METHOD, "xml");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");
        out.setOutputFile(new File("src/output.xml"));
        XsltTransformer trans = exp.load();
        trans.setInitialTemplate(new QName("main"));
        trans.setDestination(out);
        trans.transform();

        System.out.println("Output written to text file");
    }
}

2 个答案:

答案 0 :(得分:2)

将文本转换为XML的代码依赖于XSLT 2.0版和像Saxon 9这样的XSLT 2.0处理器。您尝试使用的JAXP API仅适用于以XML输入文档为主要来源的XSLT 1.0方法到XSLT代码。因此,如果要使用该API,则需要确保将虚拟输入XML传递给转换器,而纯文本文件的URI应作为参数传递。不过,我建议使用Saxon S9 API简单地使用命名模板main启动样式表,同时将纯文本URI作为参数传递。

答案 1 :(得分:1)

您无法将纯文本提供给XSL转换器。它只接受格式良好的XML作为输入。

因此链接问题中的代码在没有输入的情况下启动变换器,然后在XSLT内部加载

的文本
<xsl:variable name="csv" select="unparsed-text($pathToCSV, $encoding)" />