How to get specific XML elements with specific attribute value?(如何获取具有特定属性值的特定 XML 元素?)
问题描述
我正在尝试通过获取参数 type_id="4218"?? 的所有<Type>
"元素从 URL 解析 XML 文件?
XML 文档:
<BSQCUBS Version="0.04" Date="Fri Dec 9 11:43:29 GMT 2011" MachineDate="Fri, 09 Dec 2011 11:43:29 +0000"><类class_id="385"><标题>足球比赛</标题><类型 type_id="4264" type_minbet="0.1" type_maxbet="2000.0">...</类型><类型 type_id="5873" type_minbet="0" type_maxbet="0">...</类型><类型 type_id="4725" type_minbet="0.1" type_maxbet="2000.0">...</类型><类型 type_id="4218" type_minbet="0.1" type_maxbet="2000.0">...</类型><类型 type_id="4221" type_minbet="0.1" type_maxbet="2000.0">...</类型><类型 type_id="4218" type_minbet="0.1" type_maxbet="2000.0">...</类型><类型 type_id="4299" type_minbet="0.1" type_maxbet="2000.0">...</类型></类></BSQCUBS>
这是我的 Java 代码:
DocumentBuilder db = dbf.newDocumentBuilder();文档 doc = db.parse(new URL("http://cubs.bluesq.com/cubs/cubs.php?action=getpage&thepage=385.xml").openStream());doc.getDocumentElement().normalize();NodeList nodeList = doc.getElementsByTagName("Type");System.out.println("ukupno:"+nodeList.getLength());if (nodeList != null && nodeList.getLength() > 0) {for (int j = 0; j < nodeList.getLength(); j++) {元素 el = (org.w3c.dom.Element) nodeList.item(j);type_id = Integer.parseInt(el.getAttribute("type_id"));System.out.println("类型id:"+type_id);}}
这段代码给了我所有元素,我不想要那个,我想要属性 type_id = "4218" 的所有元素!
XPath 是你的正确选择:
DocumentBuilderFactory 工厂 = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse("<你的 xml doc uri>");XPathFactory xPathfactory = XPathFactory.newInstance();XPath xpath = xPathfactory.newXPath();XPathExpression expr = xpath.compile("//Type[@type_id="4218"]");NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
并遍历nl
I am trying to parse an XML file from a URL by taking all "<Type>
" elements where is parameter type_id="4218"??
XML document:
<BSQCUBS Version="0.04" Date="Fri Dec 9 11:43:29 GMT 2011" MachineDate="Fri, 09 Dec 2011 11:43:29 +0000">
<Class class_id="385">
<Title>Football Matches</Title>
<Type type_id="4264" type_minbet="0.1" type_maxbet="2000.0">
...
</Type>
<Type type_id="5873" type_minbet="0" type_maxbet="0">
...
</Type>
<Type type_id="4725" type_minbet="0.1" type_maxbet="2000.0">
...
</Type>
<Type type_id="4218" type_minbet="0.1" type_maxbet="2000.0">
...
</Type>
<Type type_id="4221" type_minbet="0.1" type_maxbet="2000.0">
...
</Type>
<Type type_id="4218" type_minbet="0.1" type_maxbet="2000.0">
...
</Type>
<Type type_id="4299" type_minbet="0.1" type_maxbet="2000.0">
...
</Type>
</Class>
</BSQCUBS>
Here is my Java code:
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL("http://cubs.bluesq.com/cubs/cubs.php?action=getpage&thepage=385.xml").openStream());
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Type");
System.out.println("ukupno:"+nodeList.getLength());
if (nodeList != null && nodeList.getLength() > 0) {
for (int j = 0; j < nodeList.getLength(); j++) {
Element el = (org.w3c.dom.Element) nodeList.item(j);
type_id = Integer.parseInt(el.getAttribute("type_id"));
System.out.println("type id:"+type_id);
}
}
This code gives me all elements, I don't want that, I want all elements where the attribute type_id = "4218"!
XPath is right choice for you:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("<Your xml doc uri>");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//Type[@type_id="4218"]");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
And iterate through nl
这篇关于如何获取具有特定属性值的特定 XML 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取具有特定属性值的特定 XML 元素?
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01