I'm trying to parse the xml string
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. Any advise on this would be helpful.Thanks in advance.
StringBufferInputStreamis deprecated, use aStringReaderinstead.