user7084787
user7084787

Reputation: 23

I am not able to parse IOS driver page source

I got Page source using

String pageSource = driver.getPageSource();

Now i need to save this xml file to local in cache. So i need to get element attributes like x and y attribute value rather than every time get using element.getAttribute("x");. But I am not able to parse pageSource xml file to some special character. I cannot remove this character because at if i need element value/text it shows different text if i will remove special character. Appium is use same way to do this.

Upvotes: 1

Views: 598

Answers (2)

user7084787
user7084787

Reputation: 23

I can able to do same using SAXParser and add handler to do for this. Refer SAX Parser

Upvotes: 1

bangoria anjali
bangoria anjali

Reputation: 400

I was also facing same issue and i got resolution using below code which i have written and it works fine

public static void removeEscapeCharacter(File xmlFile) {

    String pattern = "(\\\"([^=])*\\\")";
    String contentBuilder = null;
    try {
        contentBuilder = Files.toString(xmlFile, Charsets.UTF_8);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    if (contentBuilder == null)
        return;
    Pattern pattern2 = Pattern.compile(pattern);
    Matcher matcher = pattern2.matcher(contentBuilder);
    StrBuilder sb = new StrBuilder(contentBuilder);

    while (matcher.find()) {

        String str = matcher.group(1).substring(1, matcher.group(1).length() - 1);
        try {
            sb = sb.replaceFirst(StrMatcher.stringMatcher(str),
                    StringEscapeUtils.escapeXml(str));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    try {
        Writer output = null;
        output = new BufferedWriter(new FileWriter(xmlFile, false));

        output.write(sb.toString());
        output.close();

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

if you will get that kind of problem then catch it with remove special character and parse again.

    try {
            doc = db.parse(fileContent);
        } catch (Exception e) {
            removeEscapeCharacter(file);

            doc = db.parse(file);
        }

It might works for you.

Upvotes: 3

Related Questions