0

我知道这可能很简单,但是我似乎找不到我做错了什么。

我正在将网页的输出与文本文件的内容进行比较。我有以下代码,似乎对于网页的每个结果,bufferedreader 都会循环一次。一旦我发布代码和结果,这将不那么令人困惑。

代码

String docOne = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div/div/h5")).getAttribute("textContent").toString();
String docTwo = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[2]/div/h5")).getAttribute("textContent").toString();
String docThree = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[3]/div/h5")).getAttribute("textContent").toString();
String docFour = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[4]/div/h5")).getAttribute("textContent").toString();
String docFive = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[5]/div/h5")).getAttribute("textContent").toString();
String docSix = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[6]/div/h5")).getAttribute("textContent").toString();
String docSeven = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[7]/div/h5")).getAttribute("textContent").toString();
String docEight = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div/div/div[2]/div/div[8]/div/h5")).getAttribute("textContent").toString();
String docNine = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div/div[4]/div[2]/div/div/div[2]/div/div[2]/div/div/div/h5")).getAttribute("textContent").toString();

try (BufferedReader br = new BufferedReader(new FileReader("/Users/vhaislsalisc/Documents/CDWProductionDomainsRequiredDocs.txt"))) {
    String sCurrentLine;


    while ((sCurrentLine = br.readLine()) != null) {

        System.out.println("Webpage: " + docOne + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docTwo + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docThree + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docFour + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docFive + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docSix + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docSeven + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docEight + " Required Doc: " + sCurrentLine);
        System.out.println("Webpage: " + docNine + " Required Doc: " + sCurrentLine);
    }
    br.close();
} catch (IOException e) {
    System.err.println("Error: " + e.getMessage());
}

结果

Webpage: Research Request Memo (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Research Study Institutional Review Board (IRB) Approval Letter (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Sample Informed Consent and HIPAA Authorization (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Research and Development (RD) Committee Approval Letter (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Data Use Agreement (File Document) Required Doc: Research Request Memo (File Document)
Webpage: IRB Approval of Waiver of HIPAA-Compliant Authorization (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Research Protocol (File Document) Required Doc: Research Request Memo (File Document)
Webpage: CDW-Domain Checklist (File Document) Required Doc: Research Request Memo (File Document)
Webpage: VA Form 9957 (File Document) Required Doc: Research Request Memo (File Document)
Webpage: Research Request Memo (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Research Study Institutional Review Board (IRB) Approval Letter (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Sample Informed Consent and HIPAA Authorization (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Research and Development (RD) Committee Approval Letter (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Data Use Agreement (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: IRB Approval of Waiver of HIPAA-Compliant Authorization (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: Research Protocol (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: CDW-Domain Checklist (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)
Webpage: VA Form 9957 (File Document) Required Doc: Research Study Institutional Review Board (IRB) Approval Letter (File Document)

等等通过文件的其余部分

所以只是想知道我做了什么菜鸟,以及如何修复它,以便它只通过文件一次并且所有内容都与来自网页的内容相匹配?

4

1 回答 1

2

对于文本文件中的每一行,它都会遍历从 docOne 到 docNine 的每个网页。即它将所有九个文件与第 1 行进行比较,然后与第 2 行进行比较,依此类推。

由于您已经粘贴了从文本文件中读取的前 2 行,并且它们与前 2 个网页匹配,因此我假设您想要读取每一行一次并将其与一个网页进行比较。然后你需要做的只是在显示每个结果后丢失 while 循环并调用 readline:

    sCurrentLine = br.readLine();
    System.out.println("Webpage: " + docOne + " Required Doc: " + sCurrentLine);
    sCurrentLine = br.readLine();
    System.out.println("Webpage: " + docTwo + " Required Doc: " + sCurrentLine);

但是,这会使您的代码比现在更难阅读。您应该考虑改用 Collection(甚至是数组)。

[编辑] 根据 gparyani 的建议,您不需要br.close()显式调用。详细信息在这里http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

于 2013-08-15T22:24:12.443 回答