2016-10-28 46 views
1

我使用我無法解析IOS驅動程序頁面的源代碼

String pageSource = driver.getPageSource(); 

現在我需要把這個XML文件保存到本地緩存頁面源。所以我需要獲取像x和y屬性值這樣的元素屬性,而不是每次使用element.getAttribute("x");。但我無法將pageSource xml文件解析爲某些特殊字符。我不能刪除這個字符,因爲如果我需要元素值/文本,它會顯示不同的文本,如果我將刪除特殊字符。 Appium使用相同的方式來執行此操作。

回答

3

我也面臨着同樣的問題,我得到了使用下面的代碼,我寫的分辨率,它工作正常

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

,如果你會得到那樣的問題,然後趕上它刪除特殊字符,並再次解析。

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

      doc = db.parse(file); 
     } 

它可能適合你。

+0

它的工作原理。非常感謝你。 – user7084787

1

我可以使用SAXParser來做同樣的事情,併爲此添加處理程序。 參考SAX Parser

+0

是的,沒錯。大 –

相關問題