1

Here's the requirement

I have a xml document which looks something like this

-<page height="777" width="777">
    -<block r="777" l="777" blockType="Separator">
        +<region>
        +<separator>
    -<block r="777" l="777" blockType="Separator">
        +<region>
        +<separator>
    -<block r="777" l="777" blockType="Text">
        +<region>
        +<text>
</page>     

I have all the separator blocks in separatorNodeList

    String expression = "//page/block/separator";
    XPathExpression expr = xPath.compile(expression);
    NodeList separatorNodeList = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);

Now, i am trying to get the attribute(r,l) value of parent node of separator block.i.e. something like

int separatorDistanceFromRight = Integer.parseInt(((Element)separatorNodeList.item(i)).getParentNode().getAttribute("r"));

But the above doesn't seem to work. Any Quick help ??

Maverick
  • 1,396
  • 5
  • 22
  • 42
  • 1
    Try to separate your expression into variables, and see which part return unexpected value; put `(Element)separatorNodeList.item(i)` in a variable, `.getParentNode()` in a variable, so on. – har07 Jun 09 '15 at 00:38
  • Please elaborate on "doesn't seem to work". Is there a compilation error? runtime error? Does it return the wrong value? If so, what value does it return? http://meta.stackexchange.com/questions/91040/auto-detect-the-phrase-it-doesnt-work-and-ask-user-if-enough-info-given – LarsH Jun 09 '15 at 14:16

1 Answers1

1

Ahh,i found this.

int separatorDistanceFromRight = Integer.parseInt(separatorNodeList.item(i).getParentNode().getAttributes().getNamedItem("r").getNodeValue());
Maverick
  • 1,396
  • 5
  • 22
  • 42