的标签的总和? package com-6ren">
gpt4 book ai didi

java - 计算 XAML 文件中标签的数量

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

我有一个 XAML 文件,其中标签如下:

<ActorName Name="Nancy" Enabled="False"> 
<ActorName Name="Shivani" Enabled="True">

我已附上下面的代码,该代码正在计算这些标签,但我无法对其进行总结。如何返回所有具有 Enabled="True"> 的标签的总和?

package com.test;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Test {

public static void main(String[] args) {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder;

try {
builder = factory.newDocumentBuilder();

Document doc=builder.parse("C:\\Users\\nprabhasini\\Desktop\\ShivaSoft.xml");

NodeList nodes = doc.getElementsByTagName("ActorName");

for(int i=0;i<nodes.getLength();i++)
{
printNodeInfo(nodes.item(i));
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static void printNodeInfo(Node node)
{

if(node.hasAttributes())
{
NamedNodeMap rootAtt = node.getAttributes();

for(int i=0;i<rootAtt.getLength();i++)
{

String s1=rootAtt.item(i).getNodeValue();
if(s1.equalsIgnoreCase("True"))
{
i++;
System.out.println(i);
}
}
}
}

static String nodeType(short type) {

switch(type) {
case Node.ELEMENT_NODE: return "Element";
case Node.ATTRIBUTE_NODE: return "Attribute";
}

return "Unidentified";
}
}

最佳答案

您可以使用简单的 XPath-1.0 表达式,例如

count(//ActorName[@Enabled='True'])

使用 Java。只需根据您的需要调整下面的 XPathExpression 即可。当然,文件名可以是 sample.xaml 或类似名称。
这是示例代码:

import java.io.File;
import java.io.IOException;

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.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class a {

public static void main(String[] args) {
try {
File inputFile = new File("sample.xml");
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 XPathExpression = "count(//ActorName[@Enabled='True'])";
// Get the count of the elements with the given properties
Number cnt = (Number)xPath.compile(XPathExpression).evaluate(doc, XPathConstants.NUMBER);
// Convert the Float value to an int value and output
System.out.println("Items: "+cnt.intValue());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
}

关于java - 计算 XAML 文件中标签的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61463456/

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