gpt4 book ai didi

java - 计算所有项目并显示在控制台上

转载 作者:行者123 更新时间:2023-12-01 16:51:13 25 4
gpt4 key购买 nike

我有一个sample.txt 文件,其中包含XML 文件的列表URL。在我的程序中,我在 example.txt 的帮助下读取所有这些 XAML,并且我必须在这些 XMLS 中搜索特定标签。我能够找到单个 xaml 文件中的标签数量,但无法对所有标签进行求和。有人可以帮我解决下面的代码吗?例如,Sample.txt 文件包含 3 个 xml,每个 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 变量,如 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);

关于java - 计算所有项目并显示在控制台上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61683765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com