[英]How can I sum all numbers with a certain tag in an XML file?
以下是示例XML文件中的幾行內容:
<media:content url="randomurl1" duration="51" medium="video"/>
<media:content url="randomurl2" duration="28" medium="video"/>
<media:content url="randomurl3" duration="72" medium="video"/>
我想對所有持續時間值求和,例如51、28和72。 有什么整潔的方法可以做到這一點嗎? 我對此很陌生。 非常感謝您提供的所有幫助!
您可以使用以下代碼對JAVA中的xml元素求和
double sum = 0.0;
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) nNode;
sum += Double.parseDouble(eElement.getElementsByTagName("duration").item(0).getTextContent());
}
}
System.out.println("Total: " + sum);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.