负责从 web txt 中获取数据的代码行的正确名称是什么?

What is a proper name for lines of code responsible for fetching data from web txt?

我需要帮助来划分这些代码行并将它们放入方法中:

   url = new URL(URL_SOURCE);
   con = url.openConnection();
  is = con.getInputStream();
   DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
   NodeList nList = doc.getElementsByTagName("pozycja");

我分为:

 public URLConnection openConnectionOfGivenURL(String givenURL) throws IOException {
    URL url = new URL(givenURL);
    return url.openConnection();
}

剩下的不知道怎么办。我应该用 getDOM 命名吗?

我认为除了第一行和最后一行之外的所有内容都应该在一个方法中。不要试图将 that 代码分割得更远。例如。 Document getXml(URL url) 或者如果您只打算将其与 HTTP(S) url 一起使用,则可以将其命名为 downloadXml

不进一步划分的主要原因是您应该使用 try-with-resources。

此外,您不需要对已解析的 DOM 进行规范化,因为 .

Document getXml(URL url) {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    URLConnection con = url.openConnection();
    try (InputStream is = con.getInputStream()) {
        return dBuilder.parse(is);
    }
}

然后你这样使用它:

URL url = new URL(URL_SOURCE);
Document doc = getXml(url);
NodeList nList = doc.getElementsByTagName("pozycja");