[java开发篇][dom4j模块]遍历,解析xml

package com.softwinner.performance.benchmark;

/**
 * Created by Administrator on 2017/7/21.
 */

import org.dom4j.Element;


/**
 * 数据实体类
 *
 * @author*/
public class XMLBean {

    public final Element element;
    public final int index;
    public final String text;
    public final String resourceId;
    public final String classText;
    public final String packageText;
    public final String contentDesc;
    public final boolean checkable;
    public final boolean checked;
    public final boolean clickable;
    public final boolean enable;
    public final boolean focusable;
    public final boolean focused;
    public final boolean scrollable;
    public final boolean long_clickable;
    public final boolean password;
    public final boolean selected;
    public final String bounds;

    public XMLBean(Element element) {
        this.element = element;

        index = Integer.parseInt(element.attributeValue("index"));
        text = element.attributeValue("text");
        resourceId = element.attributeValue("resource-id");
        classText = element.attributeValue("class");
        packageText = element.attributeValue("package");
        contentDesc = element.attributeValue("content-desc");
        checkable = Boolean.parseBoolean(element.attributeValue("checkable"));
        checked = Boolean.parseBoolean(element.attributeValue("checked"));
        clickable = Boolean.parseBoolean(element.attributeValue("clickable"));
        enable = Boolean.parseBoolean(element.attributeValue("enabled"));
        focusable = Boolean.parseBoolean(element.attributeValue("focusable"));
        focused = Boolean.parseBoolean(element.attributeValue("focused"));
        scrollable = Boolean.parseBoolean(element.attributeValue("scrollable"));
        long_clickable = Boolean.parseBoolean(element.attributeValue("long-clickable"));
        password = Boolean.parseBoolean(element.attributeValue("password"));
        selected = Boolean.parseBoolean(element.attributeValue("selected"));
        bounds = element.attributeValue("bounds");

    }

    @Override
    public String toString() {
        return "XMLBean [index=" + index + ", text=" + text + ", resourceId=" + resourceId + ", classText="
                + classText + ", packageText=" + packageText + ", contentDesc=" + contentDesc
                + ", checkable=" + checkable + ", checked=" + checked + ", clickable=" + clickable
                + ", enable=" + enable + ", focusable=" + focusable + ", focused=" + focused
                + ", scrollable=" + scrollable + ", long_clickable=" + long_clickable + ", password="
                + password + ", selected=" + selected + ", bounds=" + bounds + "]";
    }

}

 

package com.softwinner.performance.benchmark;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
import android.util.Log;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * Created by Administrator on 2017/7/21.
 */

public class test {

    private static List<XMLBean> list;
    private static final String PATH = "/sdcard/window_dump.xml";
    private final static int LAUNCH_TIMEOUT = 5000;
    UiDevice device;
    private final String mLogTag = "testliuzhipeng";


    @Before
    public void setup() throws InterruptedException {
        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        this.grantPermission();
        openApplication("com.antutu.videobench");
        Thread.sleep(20000);
        device.waitForIdle();
        Log.i(mLogTag, "sleep finished");
    }

    @Test
    public void testAA() throws UiObjectNotFoundException, IOException, InterruptedException {

        File file = new File(PATH);
        if(file.exists()){
            assertTrue(String.format("delete %s fail ",PATH), file.delete());
        }
        device.dumpWindowHierarchy(file);
        if (!file.exists()) {
            Log.i(mLogTag, "dump fail");
            assertTrue("dump fail", false);
        }
        try {
            list = new LinkedList<XMLBean>();
            // 1、读取xml文件
            SAXReader reader = new SAXReader();
            Document document = reader.read(file);
            // 2、获取根节点
            Element rootNode = document.getRootElement();
            treeWalk(rootNode);
            Log.i(mLogTag, "list::" + list.toString());
            Log.i(mLogTag, "list.size::" + list.size());
            if (list != null)
                list.clear();
            list = null;
            Log.i(mLogTag, "End");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void print(Object obj) {
        System.out.println(obj);
    }

    private void treeWalk(Element element) throws UiObjectNotFoundException {
        String text;
        for (Iterator i = element.elementIterator(); i.hasNext(); ) {
            Element elementNode = (Element) i.next();
            if (elementNode.elements().size() > 0) {
                treeWalk(elementNode);
            }
            print(elementNode.toString());
            Log.i(mLogTag, elementNode.toString());
            XMLBean bean = new XMLBean(elementNode);
            print("-------------------------------------------");
            Log.i(mLogTag, "-------------------------------------");
            if (bean.text.equals("Play Music")) {
                print(bean.bounds);
                Log.i(mLogTag, bean.bounds);
            }
            print("-------------------------------------------");
            list.add(bean);
        }
    }
    public void openApplication(String packageNameStr){
//        wakeScreen();
        /* Start from the home screen*/
        device.pressHome();

        final String launcherPackage = device.getLauncherPackageName();
        assertThat(launcherPackage,notNullValue());
        device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
                LAUNCH_TIMEOUT);

        // launch the app
        Context context = InstrumentationRegistry.getContext();
        final Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(packageNameStr);
        // Clear out any previous instances
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);

        // Wait for the app to appear
//        try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}
//        wakeScreen();
        device.wait(Until.hasObject(By.pkg(packageNameStr).depth(0)),
                LAUNCH_TIMEOUT);
    }
    public void grantPermission(){
        openApplication("com.softwinner.performance.frameratetest");
        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        Log.i(mLogTag,"after open test apk success, grant stored permission");
//        wakeScreen();
        UiObject permission = device.findObject(new UiSelector()
                .resourceId("com.android.packageinstaller:id/permission_allow_button")
        );
        if(permission.waitForExists(1000)){
            try {
//                wakeScreen();
                permission.clickAndWaitForNewWindow();
                Log.i(mLogTag,"click grant store permission");
            } catch (UiObjectNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public void printMessage(final InputStream input){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Reader reader = new InputStreamReader(input);
                BufferedReader bf = new BufferedReader(reader);
                String line = null;
                try{
                    while((line = bf.readLine()) != null ){
                       Log.i(mLogTag, line);
                    }
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }
        }).start();

    }
}

 

posted @ 2017-07-25 10:20  liuzhipenglove  阅读(1276)  评论(0编辑  收藏  举报