如何使用Java识别XML中节点的xpath?

时间:2015-06-26 07:26:52

标签: java xml xpath

我在树视图中填充了一个XML。当我单击某个特定节点时,我需要获取该特定节点的XPath。示例XML发布在下面。我曾尝试编写一些代码,但我没有得到确切的路径。请帮我解决。

    <?xml version="1.0" encoding="UTF-8"?>
<hierarchy rotation="0">
<android.widget.FrameLayout index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" instance="0">
<android.widget.LinearLayout index="0" text="" resource-id="" class="android.widget.LinearLayout" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" instance="0">
<android.widget.FrameLayout index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" instance="1">
<android.widget.TextView index="0" text="Tank" resource-id="android:id/title" class="android.widget.TextView" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" instance="0"/>
</android.widget.FrameLayout>
<android.widget.FrameLayout index="1" text="" resource-id="android:id/content" class="android.widget.FrameLayout" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,100][768,1184]" instance="2">
<android.widget.LinearLayout index="0" text="" resource-id="com.test.Test:id/loginView" class="android.widget.LinearLayout" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,100][768,1184]" instance="1">
<android.widget.ImageView index="0" text="" resource-id="" class="android.widget.ImageView" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[106,120][661,291]" instance="0"/>
<android.widget.LinearLayout index="1" text="" resource-id="" class="android.widget.LinearLayout" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,291][768,407]" instance="2">
<android.widget.EditText index="0" text="Username" resource-id="com.test.Test:id/usernameTextField" class="android.widget.EditText" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="true" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[20,311][748,407]" instance="0"/>
</android.widget.LinearLayout>
<android.widget.LinearLayout index="2" text="" resource-id="" class="android.widget.LinearLayout" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,407][768,523]" instance="3">
<android.widget.EditText NAF="true" index="0" text="" resource-id="com.test.Test:id/passwordTextField" class="android.widget.EditText" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="true" password="true" selected="false" bounds="[20,427][748,523]" instance="1"/>
</android.widget.LinearLayout>
<android.widget.Button index="3" text="Login" resource-id="com.test.Test:id/loginButton" class="android.widget.Button" package="com.test.Test" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[224,543][544,639]" instance="0"/>
</android.widget.LinearLayout>
</android.widget.FrameLayout>
</android.widget.LinearLayout>
</android.widget.FrameLayout>
</hierarchy>

我的代码如下所示:

public String getXpath(String name, String bounds, Element e) {
for (Object str : e.elements()) {
            Element child = (Element) str;
            if(map.containsKey(child.getName())){
                map.put(child.getName(),(map.get(child.getName()))+1);
            }else{
                map.put(child.getName(),1);
            }
            xpath=xpath+child.getName()+"["+map.get(child.getName())+"]/";

            map.clear();
            getXpath(name, bounds, child);
        }
        map.clear();
}

我需要xpath输出,如下所示

//android.widget.LinearLayout[1]/android.widget.FrameLayout[2]/android.widget.LinearLayout[1]/android.widget.Button[1]

1 个答案:

答案 0 :(得分:1)

获取您想要获得的Node xpath并应用此功能获取其xpath

private String getXPath(Node node) {
    Node parent = node.getParent();
    if (parent == null) {
        return "/" + node.getTagName();
    }
    return getXPath(parent) + "/" + node.getTagName();
}