如何从子节点列表中获取父节点的属性值。(XPath)

How to fetch attribute value of parent node from child nodelist.(XPath)

这是要求

我有一份 xml 文档,看起来像这样

-<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>     

我在 separatorNodeList

中有所有的分隔符块
    String expression = "//page/block/separator";
    XPathExpression expr = xPath.compile(expression);
    NodeList separatorNodeList = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);

现在,我正在尝试获取分隔符 block.i 的 父节点 属性(r,l)值 .e.像

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

但是上面的方法好像不行。任何快速帮助?

啊,我找到了这个。

int separatorDistanceFromRight = Integer.parseInt(separatorNodeList.item(i).getParentNode().getAttributes().getNamedItem("r").getNodeValue());