public static NodeList selectNodeList(Node contextNode,
java.lang.String str)
throws TransformerExceptionUse an XPath string to select a nodelist. XPath namespace prefixes are resolved from the contextNode.
Parameters:
contextNode - The node to start searching from.
str - A valid XPath string.
Returns:
A NodeIterator, should never be null
java.lang.String str)
throws TransformerExceptionUse an XPath string to select a nodelist. XPath namespace prefixes are resolved from the contextNode.
Parameters:
contextNode - The node to start searching from.
str - A valid XPath string.
Returns:
A NodeIterator, should never be null
解决方案 »
- 关于移除xml之中><之间的空格
- java输出马走日的路径问题
- 在JAVA中什么是对象的绑定?
- 开帖讨论 -- 规则引擎 (up有分)
- 谁能提供《精通EJB》第二版的源程序?
- google,百度等搜索引擎的原理(如何提高公司网站访问量)?
- 寻JBuild7的安装序号,我的不管用,谢谢
- 我是新手,请各位大虾推荐几本JSP和SERVLET的书或文章吧!
- 哪里有J2EE开发软件(不是JBUILDER or Visual age)及其发布软件下载?
- Hibernate4+springmvc4 aop配置事务不回滚。100%结贴,在线等,急急急急....
- 那位大虾详细解释下Struct,Sprint,hiberate,EJB3.0是概念呀?
- 关于class
File file = new File("your xml file");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(file); // Select article titles into DOM node list.
NodeList choice = XPathAPI.selectNodeList(doc, XPATH); // Iterate over node list and print
for (int i = 0; i < choses.getLength(); i++) {
System.out.println(choses.item(i).getNodeValue());
}其中xpath参考DarrenWang(达伦)所说的
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(file);
NodeList choice = XPathAPI.selectNodeList(doc, XPATH); // Iterate over node list and print
for (int i = 0; i < choice.getLength(); i++) {
System.out.println(choses.item(i).getNodeValue());
}
呵呵
choice
可以通过这个XML来看出,其实是一个选择题的XML数据文件。
我选择的策略是:
1。是Exam 的 id ,即第几题,然后在从中选择,Body(题目内容)和Choice(选择的4个问题)
如果直接/Choice[2]那么会返回许多节点,当然在程序在选择也是可以的,我希望能够用XPath来解决这个部分,如Body就可以直接通过XPath来解决。
"//Exam[normalize-space(@id)='" + examId + "']/Body"
我希望Choice也能通过这种方法来解决。
---------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<project>
<Exam id="1">
<Body>
This is the body of no1
</Body>
<Choice name="A">
Choice A1
</Choice>
<Choice name="B">
Choice B1
</Choice>
<Choice name="C">
Choice C1
</Choice>
<Choice name="D">
Choice D1
</Choice>
</Exam>
<Exam id="2">
<Body>
This is the body of no2
</Body>
<Choice name="A">
Choice A2
</Choice>
<Choice name="B">
Choice B2
</Choice>
<Choice name="C">
Choice C2
</Choice>
<Choice name="D">
Choice D2
</Choice>
</Exam>
</project>
1。选择第一题的所有选项的内容。
String XPATH = "//Exam[@id='1']/Body/text()| //Exam[@id='1']/Choice/text() ";
输出结果:
This is the body of no1Choice A1Choice B1
Choice C1
Choice D1
2。选择第一题A选项的内容
String XPATH = "//Exam[@id='1']/Body/text()| //Exam[@id='1']/Choice[@name='A']/text() ";
输出结果:
This is the body of no1Choice A1
#################################################################
以上结果已调试。
不知道能不能解决楼主的问题。
其中的x为整数,1代表第一个choice元素,你想检索第n个choice就设x为n
给你个网址看看http://www.knowsky.com/3006.html
我的参数是examId和ChoiceNumber,
比如examId = 2, ChoiceNumber = D
应该返回,Choice D2
String xPath = "//Exam[normalize-space(@name)='" + examId + "']/child::Choice[normalize-space(@name)='" + choiceNumber + "']";