gpt4 book ai didi

java - Xpath 2.0 函数无法在使用 Saxon 的 Java 中工作

转载 作者:行者123 更新时间:2023-12-01 09:05:19 24 4
gpt4 key购买 nike

我正在编写一个简单的代码,使用 selenium 和 xpath2.0 函数从网页中抓取数据。

由于 Selenium 仅支持 xpath1.0 函数,因此我尝试使用 Saxon.jar

  1. 我已下载 Saxon9he.jar 文件并将其解压到路径“C:\Program Files\Java\jre1.8.0_111\lib\ext”
  2. 我创建了一个文件“jaxp.properties”,其中包含以下几行: javax.xml.transform.TransformerFactory = net.sf.saxon.TransformerFactoryImpl javax.xml.xpath.XPathFactory","net.sf.saxon.xpath.XPathFactoryImpl
  3. 还将我的 jar 文件包含在 eclipse 库中。

但是,我无法使用 Xpath2.0 函数获取值。

在我的代码中,如果我使用

XPathFactory factory = XPathFactory.newInstance();

而不是

XPathFactory factory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);

我能够使用xpath1.0的功能。但我需要Xpath2.0功能。请在这方面指导我。

我的代码是:

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathFactoryConfigurationException;
import javax.xml.xpath.XPathFunctionResolver;
import javax.xml.xpath.XPathVariableResolver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import net.sf.saxon.lib.NamespaceConstant;
import net.sf.saxon.xpath.XPathFactoryImpl;


public class XpathCheckClass {

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathFactoryConfigurationException, XPathExpressionException{


WebDriver dr = new FirefoxDriver();

dr.get("http://s15.a2zinc.net/clients/hartenergy/midstream17/Public/eBooth.aspx?Nav=False&BoothID=137384");
try {

Thread.sleep(3000);

} catch (Exception e) {

}

String source = dr.getPageSource();

Document doc = null;

try {

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

doc = db.parse( new InputSource( new StringReader(source)));

} catch (Exception e) {
e.printStackTrace();
}

System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_SAXON, "net.sf.saxon.xpath.XPathFactoryImpl");
XPathFactory factory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);

// XPathFactory factory = XPathFactory.newInstance(); ---> default xpath factory

XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("if(//h2) then //h2 else //h1");

NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

System.out.println(nodes.getLength());

for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getTextContent());
}


dr.close();
}

}

最佳答案

Saxon 的最新版本不再将自己宣传为 JAXP XPath 服务,因此您需要显式实例化 XPath 工厂:

XPathFactory xf = new net.sf.saxon.XPathFactoryImpl();

关于java - Xpath 2.0 函数无法在使用 Saxon 的 Java 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41326364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com