Xpath总是返回JAVA中XML的第一个子节点
我有这样一个xml:
<div class="row mt-5">
<div class="col-lg-cus col-6">
<div class="product-box lazyload-wrap">
<div class="remove-wishlist" data-id="7080">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
<div class="productAvatar">
<a href="/vi/classic-fullface-royal-m18" title="Classic FULLFACE ROYAL M18">
<div class="img img-background text-left origin product-img lazy-bg-img lazyload-item" style="background-image:url('/Uploads/default-image.jpg')" alt="Classic FULLFACE ROYAL M18" data-pil-src="https://fanfan.vn/Uploads/t/fa/fanfan0non-bao-hiem-cafe-racer-classic-fullface-royal-m18-5_0061169_235.jpg">
</div>
</a>
<a class="btn btnQuickView "
onclick="OpenCustomBootstrapModal('/vi/_Details?productId=7080', null, 1000, 'productPopup')">
<span class="txtOutOfStock">Hết hàng</span>
<span class="txtQuickView">Xem nhanh</span>
</a>
</div>
<p class="mb-1 brand-name">
<a href="/vi/royal-helmet" class="a-black" title="ROYAL HELMET">ROYAL HELMET</a>
</p>
<p class="name text-uppercase mb-0">
<a href="/vi/classic-fullface-royal-m18" class="a-black">Classic FULLFACE ROYAL M18</a>
</p>
<div class="rating my-2">
<span class="star-raty" data-score="0" data-readOnly="true"></span>
</div>
<p>
<span>1.100.000 ₫</span>
</p>
</div>
</div>
<div class="col-lg-cus col-6">
<div class="product-box lazyload-wrap">
<div class="remove-wishlist" data-id="6855">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
<div class="productAvatar">
<a href="/vi/non-bao-hiem-34-royal-m01-tem" title="Nón bảo hiểm 3/4 Royal M01 Tem">
<div class="img img-background text-left origin product-img lazy-bg-img lazyload-item" style="background-image:url('/Uploads/default-image.jpg')" alt="Nón bảo hiểm 3/4 Royal M01 Tem" data-pil-src="https://fanfan.vn/Uploads/t/fa/fanfan0mu-non-bao-hiem-3-4-di-xe-may-royal-m01-tem-helmet-with-texture-4-do-xam-red-si_0060108_235.jpg">
</div>
</a>
<a class="btn btnQuickView "
onclick="OpenCustomBootstrapModal('/vi/_Details?productId=6855', null, 1000, 'productPopup')">
<span class="txtOutOfStock">Hết hàng</span>
<span class="txtQuickView">Xem nhanh</span>
</a>
</div>
<p class="mb-1 brand-name">
<a href="/vi/royal-helmet" class="a-black" title="ROYAL HELMET">ROYAL HELMET</a>
</p>
<p class="name text-uppercase mb-0">
<a href="/vi/non-bao-hiem-34-royal-m01-tem" class="a-black">Nón bảo hiểm 3/4 Royal M01 Tem</a>
</p>
<div class="rating my-2">
<span class="star-raty" data-score="0" data-readOnly="true"></span>
</div>
<p>
<span>400.000 ₫</span>
</p>
</div>
</div>
我正在尝试解析它们,从中获取一些数据,并将它们解析到jaxb,我是如何做到这一点的:
public static void fetchFanFanData(String dataFilePath, String type) {
try {
Document doc = DocParser(dataFilePath);
XPath xpath = getXPath();
String query = "//div[@class=\"col-lg-cus col-6\"]";
NodeList list = (NodeList) xpath.evaluate(query, doc, XPathConstants.NODESET);
NodeList list = doc.getDocumentElement().getChildNodes();
System.out.println(list.getLength());
Products products = new Products();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
String url = xpath.evaluate("//p[@class=\"name text-uppercase mb-0\"]/a/@href", node, XPathConstants.STRING).toString();
String name = xpath.evaluate("//p[@class=\"name text-uppercase mb-0\"]/a", node, XPathConstants.STRING).toString();
String producer = xpath.evaluate("//p[@class=\"mb-1 brand-name\"]", node, XPathConstants.STRING).toString();
String image_url = xpath.evaluate("//div[@class=\"productAvatar\"]/a/div/@data-pil-src", node, XPathConstants.STRING).toString();
String price = xpath.evaluate("//p/span", node, XPathConstants.STRING).toString();
Product product = new Product();
product.setName(name);
product.setImage(image_url);
product.setUrl(url);
product.setPrice(price);
product.setProducer(producer);
product.setStore("FanFan");
product.setType(type);
products.getProduct().add(product);
}
marshallJAXB(products, dataFilePath);
} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | JAXBException ex) {
Logger.getLogger(XMLUtilities.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void marshallJAXB(Products products, String path) throws JAXBException, FileNotFoundException {
JAXBContext context = JAXBContext.newInstance(Products.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(products, new File(ServletActionContext.getServletContext().getRealPath("/" + "WEB-INF\\result.xml")));
}
public static XPath getXPath() {
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
return xPath;
}
public static Document DocParser(String filePath)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(filePath);
return doc;
}
marshaller只是验证jaxb是否正确,但我得到的总是第一个节点,如下所示:
<products xmlns="http://www.example.org/product">
<product type="helmet">
<name>Classic FULLFACE ROYAL M18</name>
<url>/vi/classic-fullface-royal-m18</url>
<image>https://fanfan.vn/Uploads/t/fa/fanfan0non-bao-hiem-cafe-racer-classic-fullface-royal-m18-5_0061169_235.jpg</image>
<price>1.100.000 ₫</price>
<producer>ROYAL HELMET</producer>
<store>FanFan</store>
</product>
<product type="helmet">
<name>Classic FULLFACE ROYAL M18</name>
<url>/vi/classic-fullface-royal-m18</url>
<image>https://fanfan.vn/Uploads/t/fa/fanfan0non-bao-hiem-cafe-racer-classic-fullface-royal-m18-5_0061169_235.jpg</image>
<price>1.100.000 ₫</price>
<producer>ROYAL HELMET</producer>
<store>FanFan</store>
</product>
<product type="helmet">
<name>Classic FULLFACE ROYAL M18</name>
<url>/vi/classic-fullface-royal-m18</url>
<image>https://fanfan.vn/Uploads/t/fa/fanfan0non-bao-hiem-cafe-racer-classic-fullface-royal-m18-5_0061169_235.jpg</image>
<price>1.100.000 ₫</price>
<producer>ROYAL HELMET</producer>
<store>FanFan</store>
</product>
我试过很多方法,但现在没有希望了。有人知道为什么吗?请帮忙。 我无法了解xpath在这种情况下是如何工作的,尽管我在特定的上下文中使用它们
# 1 楼答案
我想问题发生在for循环中的fetchFanData()方法中 通过访问url、名称等的值,您必须在此处将“/”替换为“/” 对于所有访问,例如更换
与
“/”和“/”之间的区别是:
从https://www.w3.org/TR/2017/REC-xpath-31-20170321/ 一般而言,尤其是第3.3.5章“示例”一节。而且 https://docs.oracle.com/javase/10/docs/api/javax/xml/xpath/package-summary.html
总是得到相同值的原因是:表达式
应用于列表的节点还将返回一个列表,其中包含整个文档中的所有点击(而不是单个点击)。此外,该列表对于每个节点都是相同的。与返回类型XPathConstants结合使用。字符串始终选择第一个(相同)命中。因此,为每个节点返回相同的结果