문제

I'm pretty confused about this one. Given the following 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>

When using JDOM using the following code:

  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));
  }

The console shows this:

 Event One
 Location One
 Event One
 Location One

What am I missing?

도움이 되었습니까?

해결책

Don't use '//' it starts always searching at the root node. Use e.g. './sch:eventName' it is relative to the current node.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top