gpt4 book ai didi

java - 如何使用java解析XML文件

转载 作者:行者123 更新时间:2023-12-02 12:32:39 25 4
gpt4 key购买 nike

我正在尝试解析 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>

使用下面的解析器

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

我收到以下错误:

[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)

我所尝试的只是通过标记名检索按钮元素。

如何使用 appium java 检索 ios 应用程序的元素。对此的任何建议都会有所帮助。提前致谢。

最佳答案

0x19 是 ASCII 控制字符 EM = 媒体结束。最简单的方法是忽略该字符。

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

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

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

关于java - 如何使用java解析XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45214458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com