具有标记名称ID的元素的XPath

时间:2017-07-05 13:33:14

标签: java xml xpath

我在检索标签名为“Id”的元素值时遇到问题 所有其他元素甚至父节点(//Product)XPath都可以正常工作,但由于某些原因我无法通过XPath获取Id元素

我尝试了几种路径查询,包括:

//Product/Id[1]/text()
//Product/Id[1]
//Product[1]/Id[1]/text()
//Product[1]/Id/text()

我怀疑“Id”是保留还是我在这里做错了什么?

...<Product>
              <Id>401057</Id>
              <ProductOrderKey>439138</ProductOrderKey>
              <EffectiveDate>20101102145002</EffectiveDate>
              <ExpiredDate>20370101000000</ExpiredDate>
              <Status>0</Status>
           </Product>
           <Product>
              <Id>420004</Id>
              <ProductOrderKey>439139</ProductOrderKey>
              <EffectiveDate>20101102145002</EffectiveDate>
              <ExpiredDate>20370101000000</ExpiredDate>
              <Status>0</Status>
           </Product>
           <Product>
              <Id>401061</Id>
              <ProductOrderKey>439140</ProductOrderKey>
              <EffectiveDate>20101102145002</EffectiveDate>
              <ExpiredDate>20370101000000</ExpiredDate>
              <Status>0</Status>
           </Product>
           <Product>
              <Id>401008</Id>
              <ProductOrderKey>439141</ProductOrderKey>
              <EffectiveDate>20101102145002</EffectiveDate>
              <ExpiredDate>20370101000000</ExpiredDate>
              <Status>0</Status>
           </Product>
           <Product>
              <Id>420000</Id>
              <ProductOrderKey>439142</ProductOrderKey>
              <EffectiveDate>20101102145023</EffectiveDate>
              <ExpiredDate>20370101000000</ExpiredDate>
              <Status>0</Status>
           </Product>
           <Service>...

1 个答案:

答案 0 :(得分:0)

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class Test {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
        File inputFile = new File("C:/product.txt");
        DocumentBuilderFactory dbFactory
                = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;

        dBuilder = dbFactory.newDocumentBuilder();

        Document doc = dBuilder.parse(inputFile);
        doc.getDocumentElement().normalize();

        XPath xPath = XPathFactory.newInstance().newXPath();

        String expression = "/Products/Product";
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node nNode = nodeList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                System.out.println("Student roll no : "
                        + eElement.getElementsByTagName("Id").item(0)
                        .getTextContent());
            }
        }
    }
}

您可以解析xml文件并获取Id标记值。我希望这对您有用。