I'm having trouble retrieving elements values with tag name "Id" All other elements and even the Parent Node ( //Product
) XPath works perfectly, but for some reason I can't get the Id elements by XPath
I've tried several path queries including:
//Product/Id[1]/text()
//Product/Id[1]
//Product[1]/Id[1]/text()
//Product[1]/Id/text()
I suspect "Id" is reserved or is that I'm doing something wrong here?
...<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>...
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());
}
}
}
}
You can parse a xml file and get the Id tag value.I hope this will be useful for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.