我正在编写一个 XPath 函数表达式来通过 CSS 查询查找元素。
这就是我所拥有的:
import javax.xml.transform.TransformerException;
import org.apache.xml.dtm.ref.dom2dtm.DOM2DTM;
import org.apache.xpath.XPathContext;
import org.apache.xpath.functions.FunctionOneArg;
import org.apache.xpath.objects.XObject;
import org.apache.xpath.objects.XString;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class CSSFinder extends FunctionOneArg {
@Override
public XObject execute(XPathContext c) throws TransformerException {
final DOM2DTM dtm = (DOM2DTM) c.getDTM(c.getContextNode());
final Document parse = Jsoup.parse(dtm.toString()); // toString returns not html.
final Elements parents = parse.select(getArg0().toString()).first().parents();
String xpath = "/";
for (final Element parent : parents) {
xpath += "/*[" + parent.elementSiblingIndex() + "]";
}
// replace this later...
return new XString("sc-login");
}
}
问题是toString
不返回html。如何获得完整的解析html?