0

使用JAR通过 apt for 安装的文件Saxon-HE并进行tagsoup解析html是单行的:

thufir@dur:~/saxon$ 
thufir@dur:~/saxon$ java -cp /usr/share/java/Saxon-HE-9.8.0.14.jar:/usr/share/java/tagsoup-1.2.1.jar net.sf.saxon.Query -x:org.ccil.cowan.tagsoup.Parser -qs:doc\(\'http://books.toscrape.com/\'\) 
<?xml version="1.0" encoding="UTF-8"?><!--[if lt IE 7]>      <html lang="en-us" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--><!--[if IE 7]>         <html lang="en-us" class="no-js lt-ie9 lt-ie8"> <![endif]--><!--[if IE 8]>         <html lang="en-us" class="no-js lt-ie9"> <![endif]--><!--[if gt IE 8]><!--><html xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml" class="no-js" lang="en-us"><!--<![endif]--><head><title>
    All products | Books to Scrape - Sandbox
..        
        <!-- Version: N/A -->

thufir@dur:~/saxon$ 
thufir@dur:~/saxon$ 

我将如何从 Java 中做到这一点? 特别是,Saxon此执行需要哪些导入? 也许使用Saxon和 JAXP 接口?

还:

http://codingwithpassion.blogspot.com/2011/03/saxon-xslt-java-example.html

4

1 回答 1

1

您可以在 saxonica.com 和 sourceforge.net 网站上的 saxon-resources 下载中找到许多使用 Java 中的 Saxon 调用转换的简单示例。

在这里很难确切知道您想要什么,因为您的命令行示例除了调用 TagSoup 解析器和序列化结果之外,没有使用 Saxon 做任何有用的事情。从 Java 中执行此操作的最简单方法是使用 JAXP 身份转换,它与 JDK 中的内置 XSLT 转换器和 Saxon 一样运行:

TransformerFactory factory = TransformerFactory.newInstance();
XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");
Source input = new SAXSource(xmlReader, new InputSource("http://books.toscrape.com/"));
Result output = new StreamResult(System.out);
factory.newTransformer().transform(input, output);

如果您想添加一些 XSLT 或 XQuery 处理,那当然是完全可能的(我总是使用 Saxon 的 s9api API,但您也可以使用 JAXP 或 XQJ),但具体取决于您想要做什么。

于 2019-01-04T06:13:31.457 回答