如何使用java解析XML文件

时间:2017-07-20 12:17:11

标签: java xml-parsing

我正在尝试解析xml字符串

dygraph

使用以下解析器

  String  xml= driver.getPageSource();
System.out.println("Xml Result :" +xml);


Xml Result:<?xml version="1.0" encoding="UTF-8"?><AppiumAUT><XCUIElementTypeApplication type="XCUIElementTypeApplication" name="SHAREit" label="SHAREit" enabled="true" x="0" y="0" width="375" height="667">
  <XCUIElementTypeWindow type="XCUIElementTypeWindow" enabled="true" x="0" y="0" width="375" height="667">
    <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="0" y="0" width="375" height="667">
      <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="0" y="0" width="375" height="667">
        <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="0" y="0" width="375" height="667">
          <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="0" y="0" width="375" height="667">
            <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="-375" y="0" width="375" height="667">
              <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="-375" y="0" width="298" height="667">
                <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="-375" y="0" width="298" height="183">
                  <XCUIElementTypeButton type="XCUIElementTypeButton" name="ic avatar 4" label="ic avatar 4" enabled="true" x="-259" y="55" width="65" height="65"/>
                  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="Cognizant’s iPhone" name="Cognizant’s iPhone" label="Cognizant’s iPhone" enabled="true" x="-367" y="129" width="282" height="19"/>
                  <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="-340" y="162" width="213" height="21">
                    <XCUIElementTypeImage type="XCUIElementTypeImage" name="ic_left_laba" enabled="true" x="-340" y="162" width="15" height="21"/>
                    <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="-320" y="162" width="193" height="21"/>
                  </XCUIElementTypeOther>
                </XCUIElementTypeOther>
<XCUIElementTypeWindow type="XCUIElementTypeWindow" enabled="true" x="0" y="0" width="375" height="667">
    <XCUIElementTypeStatusBar type="XCUIElementTypeStatusBar" enabled="true" x="0" y="0" width="375" height="20">
      <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="0" y="0" width="375" height="20"/>
      <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" x="0" y="0" width="375" height="20">
        <XCUIElementTypeOther type="XCUIElementTypeOther" name="No SIM" label="No SIM" enabled="true" x="6" y="0" width="41" height="20"/>
        <XCUIElementTypeOther type="XCUIElementTypeOther" name="5:21 PM" label="5:21 PM" enabled="true" x="165" y="0" width="48" height="20"/>
        <XCUIElementTypeOther type="XCUIElementTypeOther" name="Bluetooth on" label="Bluetooth on" enabled="true" x="323" y="0" width="8" height="20"/>
        <XCUIElementTypeOther type="XCUIElementTypeOther" name="100% battery power, On AC Power" label="100% battery power, On AC Power" enabled="true" x="337" y="0" width="33" height="20"/>
      </XCUIElementTypeOther>
    </XCUIElementTypeStatusBar>
  </XCUIElementTypeWindow>
</XCUIElementTypeApplication></AppiumAUT>

我收到以下错误:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;

import java.awt.List;
import java.io.File;
import java.io.StringBufferInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import java.util.PriorityQueue;

import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.TimeUnit;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.DocumentBuilder;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class Parser {
    Document doc=null;
    NodeList nList=null;

    //public static void main(String[] args) throws MalformedURLException {
    public ArrayList<String> XmlParser(String xml) throws TransformerException{
        ArrayList<String> buttonresourceIds=new ArrayList<String>();
        ArrayList<String> textresourceIds=new ArrayList<String>();
        try { 

            //File inputFile = new File("window_dump.xml");
            //File inputFile = new File(xml);

            DocumentBuilderFactory dbFactory 
            = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();



            try
            {
                //doc = dBuilder.parse(inputFile.toString());
                doc = dBuilder.parse(new StringBufferInputStream(xml));
                System.out.println("Document"+doc);
                //doc = dBuilder.parse((xml));
            }
            catch(Exception e)
            {
                //System.out.println(xml);
                //System.out.println(string);
                e.printStackTrace();
            }
            //doc.getDocumentElement().normalize();
            // System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

             nList = doc.getElementsByTagName("*");

            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.hasAttribute("type"))
                    {
                        if(eElement.getAttribute("type").contains("XCUIElementTypeButton")&&!(eElement.getAttribute("name").trim().equals("")))

                        {
                            System.out.println("Button Element with Id:"+eElement.getAttribute("name"));
                            //buttonresourceIds.add("resid$"+eElement.getAttribute("name"));
                            buttonresourceIds.add(eElement.getAttribute("name"));
                            //buttonresourceIds.add("resid_"+eElement.getAttribute("resource-id"));

                        } 
                        else if(eElement.getAttribute("type").contains("XCUIElementTypeStaticText")&&!(eElement.getAttribute("value").trim().equals("")))
        {
System.out.println("Button Element with Id:"+eElement.getAttribute("name"));
//buttonresourceIds.add("name_"+eElement.getAttribute("name"));
buttonresourceIds.add(eElement.getAttribute("name"));
  }

                }
            }
        }
        }
         catch (Exception e) {
            e.printStackTrace();
        }

        return buttonresourceIds;
    }

我正在尝试的是按标记名检索按钮元素。

如何使用appium java检索ios app的元素。对此提出任何建议都会有所帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

0x19是ASCII控制字符EM =媒体结束。 最简单的是忽略这个角色。

xml = xml.replaceAll("[\u0019]", "");

xml = xml.replace("\u0019", "");

如果出现其他无效控制字符,请将它们添加到方括号内。