简体   繁体   English

如何使用Java解析XML文件

[英]How to parse the XML file using java

I'm trying to parse the xml string 我正在尝试解析xml字符串

  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>

using the below parser 使用下面的解析器

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

i get shown the below error: 我得到以下错误:

[Fatal Error] :11:95: An invalid XML character (Unicode: 0x19) was found in the value of attribute "value" and element is "XCUIElementTypeStaticText".
org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 95; An invalid XML character (Unicode: 0x19) was found in the value of attribute "value" and element is "XCUIElementTypeStaticText".
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
    at Parser.XmlParser(Parser.java:59)
    at App_Crawler.driversetup(App_Crawler.java:114)
    at App_Crawler.main(App_Crawler.java:62)
java.lang.NullPointerException
    at Parser.XmlParser(Parser.java:72)
    at App_Crawler.driversetup(App_Crawler.java:114)
    at App_Crawler.main(App_Crawler.java:62)

All i'm trying is to retrieve the button elements by tagname. 我正在尝试的是按标记名检索按钮元素。

how to retrieve the elements for ios app using appium java. 如何使用appium java检索ios应用程序的元素。 Any advise on this would be helpful.Thanks in advance. 在此方面的任何建议都将有所帮助。

0x19 is ASCII control character EM = End of Media. 0x19是ASCII控制字符EM =媒体结束。 Simplest would be to ignore the character. 最简单的就是忽略字符。

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

or 要么

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

Should other invalid control characters appear, add them inside the square brackets. 如果出现其他无效的控制字符,请将其添加在方括号内。

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

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