【问题标题】:Count all the items and display on console计算所有项目并在控制台上显示
【发布时间】:2020-08-24 06:46:49
【问题描述】:

我有一个包含 XML 文件列表 URL 的 sample.txt 文件。在我的程序中,我在 sample.txt 的帮助下读取所有这些 XAML,我必须在这些 XMLS 中搜索特定标签。我能够找到单个 xaml 文件中的标签数量,但无法对所有标签求和。有人可以帮助我使用下面的代码。例如- Sample.txt 文件包含 3 个 xml,每个都有 1 个我要搜索的字符串。我得到的输出是 1,1,1 但不是 SUM=3。

package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class test3 {

    public static void main(String[] args) {
        try {

            BufferedReader reader;
            ArrayList listOfFiles = new ArrayList();
            Date startTime = new Date();
            try {
                reader = new BufferedReader(new FileReader("C:\\Desktop\\Sample.txt"));
                String line = null;
                while ((line =reader.readLine()) != null) {
                    //System.out.println(line);
                    listOfFiles.add(line);
                }
                reader.close();
                System.out.println(listOfFiles);
            } catch (IOException e) {
                e.printStackTrace();
            } 

            for(int i=0; (i<listOfFiles.size() && listOfFiles.get(i) != null) ; i++){

                URL u = new URL(listOfFiles.get(i).toString());
                URLConnection uc = u.openConnection();
                String contentType = uc.getContentType();
                int contentLength = uc.getContentLength();
                if (contentType.startsWith("text/xml") || contentLength == -1) {
                  throw new IOException("This is not a binary file.");
                }
                InputStream inputFile = uc.getInputStream();

                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                dbFactory.setValidating(false);
                DocumentBuilder dBuilder;
                dBuilder=dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(inputFile);
                doc.getDocumentElement().normalize();
                XPath xPath =  XPathFactory.newInstance().newXPath();
                String XPathExpression1 = "count(//ActorName[@Enabled='True'])"; 
                // Get the count of the elements with the given properties
                Number cnt1 = (Number)xPath.compile(XPathExpression1).evaluate(doc, XPathConstants.NUMBER);


                System.out.println("File:: "+ listOfFiles.get(i).toString() +" and Items:: "+cnt1.intValue());



            }


        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        } 
        catch (SAXException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (XPathExpressionException e) {
            e.printStackTrace();
      }
    }
}

【问题讨论】:

  • 您需要声明一个 sum 变量并在 for 循环中不断添加。
  • 你能解释一下吗
  • 我添加了一个答案。看看有没有帮助。

标签: java xml eclipse xpath


【解决方案1】:

声明一个 sum 变量,如 sum 并在循环内添加 cnt.intValue()

        int sum = 0;

        for(int i=0; (i<listOfFiles.size() && listOfFiles.get(i) != null) ; i++){

            URL u = new URL(listOfFiles.get(i).toString());
            URLConnection uc = u.openConnection();
            String contentType = uc.getContentType();
            int contentLength = uc.getContentLength();
            if (contentType.startsWith("text/xml") || contentLength == -1) {
              throw new IOException("This is not a binary file.");
            }
            InputStream inputFile = uc.getInputStream();

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setValidating(false);
            DocumentBuilder dBuilder;
            dBuilder=dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(inputFile);
            doc.getDocumentElement().normalize();
            XPath xPath =  XPathFactory.newInstance().newXPath();
            String XPathExpression1 = "count(//ActorName[@Enabled='True'])"; 
            // Get the count of the elements with the given properties
            Number cnt1 = (Number)xPath.compile(XPathExpression1).evaluate(doc, XPathConstants.NUMBER);

            sum += cnt1.intValue(); // Add to sum each time you get a count;

            System.out.println("File:: "+ listOfFiles.get(i).toString() +" and Items:: "+cnt1.intValue());



        }
       System.out.println("The sum of values is : " + sum);

【讨论】:

    猜你喜欢
    • 2017-06-11
    • 2014-06-17
    • 2021-09-06
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2017-03-13
    • 2015-04-01
    • 2014-09-23
    相关资源
    最近更新 更多