1

我对这个很困惑。给定以下xml:

<sch:eventList>
   <sch:event>
      <sch:eventName>Event One</sch:eventName>
      <sch:locationName>Location One</sch:locationName>
   </sch:event>
   <sch:event>
      <sch:eventName>Event Two</sch:eventName>
      <sch:locationName>Location Two</sch:locationName>
   </sch:event>
</sch:eventList>

使用 JDOM 时使用以下代码:

  XPath eventNameExpression = XPath.newInstance("//sch:eventName");

  XPath eventLocationExpression = XPath.newInstance("//sch:eventLocation");

  XPath eventExpression = XPath.newInstance("//sch:event");

  List<Element> elements = eventExpression.selectNodes(requestElement);
  for(Element e: elements) {
      System.out.println(eventNameExpression.valueOf(e));
      System.out.println(eventLocationExpression.valueOf(e));
  }

控制台显示:

 Event One
 Location One
 Event One
 Location One

我错过了什么?

4

1 回答 1

2

不要使用 '//' 它总是从根节点开始搜索。使用例如 './sch:eventName' 它是相对于当前节点的。

于 2010-11-03T20:22:35.227 回答