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

解决方案 »

  1.   

    /Choice[2]      传进去就可以了
      

  2.   

    当然,前面应该还有其他element,因为你给出的不是一个合适的xml,呵呵
      

  3.   

    给你一段代码参考一下:)  
          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(达伦)所说的
      

  4.   

    不好意思,上面代码有几处笔误,重新贴出:(        File file = new File("your xml file");
            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());
            }
      

  5.   

    我考,又错咯:◎#¥%◎#%
    呵呵
    choice
      

  6.   

    我的整个XML贴出来吧,谢谢楼上2位的热心回复,不过可能我的表达还不是很清晰。
    可以通过这个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>
      

  7.   

    这些天太忙了,也不知道你的问题解决没有。还是不太清楚 你是不是要这样的结果,就拿你的XML举例来说吧
    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
    #################################################################
    以上结果已调试。
    不知道能不能解决楼主的问题。
      

  8.   

    以exam元素的id属性和choice的name属性可以唯一取得你想要的数据!!!
      

  9.   

    试试这个XPath表达式:/Project/Choice [position ()=x]
    其中的x为整数,1代表第一个choice元素,你想检索第n个choice就设x为n
      

  10.   

    支持 shareworld(天阳) 的方法
    给你个网址看看http://www.knowsky.com/3006.html
      

  11.   

    试了一下楼上的几个方法,还是无法得到我想要的结果。
    我的参数是examId和ChoiceNumber,
    比如examId = 2, ChoiceNumber = D
    应该返回,Choice D2
      

  12.   

    汗还是自己解决的,用Child节点,看来我的理解不够深啊。
    String xPath = "//Exam[normalize-space(@name)='" + examId + "']/child::Choice[normalize-space(@name)='" + choiceNumber + "']";