简体   繁体   English

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

[英]How to identify xpath of a node in XML using Java?

I have an XML populated in a tree view.我在树视图中填充了一个 XML。 When I click on a particular node, I need to get the XPath for that particular node.当我单击特定节点时,我需要获取该特定节点的 XPath。 Sample XML is posted below.示例 XML 发布在下面。 I have tried writing some code, but I'm not getting the exact path.我试过写一些代码,但我没有得到确切的路径。 Please help me resolve it.请帮我解决。

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

My code looks like this:我的代码如下所示:

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

I need xpath output like below我需要像下面这样的 xpath 输出

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

Get the Node you want to get xpath and apply this function for get its xpath .获取要获取xpathNode并应用此函数获取其xpath

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM