解析android屏幕转储文件

时间:2016-06-13 10:51:53

标签: java android xml xml-parsing screenshot

我想解析这个xml文件,其中所有节点的名称都相似。



<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy>
	<node index="0" text="" class="android.widget.FrameLayout" package="com.example.amishr33.calculator1" 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,0][480,800]">
		<node index="0" text="" class="android.widget.LinearLayout" package="com.example.amishr33.calculator1" 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,0][480,800]">
			<node index="0" text="" class="android.widget.FrameLayout" package="com.example.amishr33.calculator1" 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,38][480,800]">
				<node index="0" text="" class="android.widget.RelativeLayout" package="com.example.amishr33.calculator1" 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,38][480,800]">
					<node index="0" text="" class="android.widget.TextView" package="com.example.amishr33.calculator1" 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,38][480,129]" />
					<node index="1" text="" class="android.widget.LinearLayout" package="com.example.amishr33.calculator1" 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,129][480,800]">
						<node index="0" text="" class="android.widget.LinearLayout" package="com.example.amishr33.calculator1" 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,129][480,263]">
							<node index="0" text="7" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,129][120,263]" />
							<node index="1" text="8" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[120,129][240,263]" />
							<node index="2" text="9" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[240,129][360,263]" />
							<node index="3" text="/" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[360,129][480,263]" />
						</node>
						<node index="1" text="" class="android.widget.LinearLayout" package="com.example.amishr33.calculator1" 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,263][480,397]">
							<node index="0" text="4" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,263][120,397]" />
							<node index="1" text="5" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[120,263][240,397]" />
							<node index="2" text="6" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[240,263][360,397]" />
							<node index="3" text="*" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[360,263][480,397]" />
						</node>
						<node index="2" text="" class="android.widget.LinearLayout" package="com.example.amishr33.calculator1" 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,397][480,531]">
							<node index="0" text="1" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,397][120,531]" />
							<node index="1" text="2" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[120,397][240,531]" />
							<node index="2" text="3" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[240,397][360,531]" />
							<node index="3" text="-" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[360,397][480,531]" />
						</node>
						<node index="3" text="" class="android.widget.LinearLayout" package="com.example.amishr33.calculator1" 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,531][480,665]">
							<node index="0" text="." class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,531][120,665]" />
							<node index="1" text="0" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[120,531][240,665]" />
							<node index="2" text="C" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[240,531][360,665]" />
							<node index="3" text="+" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[360,531][480,665]" />
						</node>
						<node index="4" text="=" class="android.widget.Button" package="com.example.amishr33.calculator1" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,665][480,800]" />
					</node>
				</node>
			</node>
		</node>
	</node>
</hierarchy>
&#13;
&#13;
&#13;

虽然我尝试并使用下面的代码

 SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("C:\\Users\\amishr33\\screen_dump1.uix");
        Properties btn_bound = new Properties();

        try {
            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            Element element = rootNode.getChild("node");
            Element element1 = element.getChild("node");
            Element element2 = element1.getChild("node");
            Element element3 = element2.getChild("node");
            List list = element3.getChildren("node");

            for (int i = 0; i < list.size(); i++) {

                Element node = (Element) list.get(i);
                Attribute value = node.getAttribute("class");

                if ((value.getValue().toString()).compareTo("android.widget.LinearLayout")==0) {

                    List list1 = node.getChildren("node");

                    for (int j = 0; j < list1.size(); j++) {
                        Element node1 = (Element) list1.get(j);
                        Attribute value1 = node1.getAttribute("class");

                        if ((value1.getValue().toString()).compareTo("android.widget.LinearLayout")==0) {

                            List list2 = node1.getChildren("node");

                            for (int k = 0; k < list2.size(); k++) {
                                Element node2 = (Element) list2.get(k);
                                Attribute class1 = node2.getAttribute("class");
                                Attribute text1 = node2.getAttribute("text");
                                Attribute bounds1 = node2.getAttribute("bounds");

                                btn_bound.put(new String(class1.getValue()+"."+text1.getValue()), bounds1.getValue());
                            }

                        }else{
                            Attribute class1 = node1.getAttribute("class");
                            Attribute text1 = node1.getAttribute("text");
                            Attribute bounds1 = node1.getAttribute("bounds");

                            btn_bound.put(new String(class1.getValue()+"."+text1.getValue()), bounds1.getValue());
                        }
                    }
                }
            }

        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }

但它仅对此xml文件/屏幕转储非常具体。如何使这个解析器通用?我试图通过文本属性和类属性的组合来按顺序排列它们。

1 个答案:

答案 0 :(得分:1)

我进一步尝试并发现jdom解析器是SAX paresr的更好替代品。

 try {
             File inputFile = new File(value);
             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
             Document doc = dBuilder.parse(inputFile);
             doc.getDocumentElement().normalize();

             NodeList nList = doc.getElementsByTagName("node");

             for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                   Element eElement = (Element) nNode;
                   if(eElement.getAttribute("class").toString().compareTo("android.widget.Button")==0)
                   {
                   btn_bound.put(new String(eElement.getAttribute("resource-id")), eElement.getAttribute("bounds"));
                   }
                }
             }
          } catch (Exception e) {
             e.printStackTrace();
          }