Java:计算.XML中的一个特殊字母或数字
我想问一下,我是否可以数一个特殊的字母或数字,例如: “A:” 从一个.xml文件? 把它写成一个变量 例如: foo.xml:Java:计算.XML中的一个特殊字母或数字,java,xml,file,variables,counter,Java,Xml,File,Variables,Counter,我想问一下,我是否可以数一个特殊的字母或数字,例如: “A:” 从一个.xml文件? 把它写成一个变量 例如: foo.xml: <questions> <question> <variante> A: variante1 </variante> <variante> B: variante2 </variante> </question> <question> <variante> A:
<questions>
<question>
<variante> A: variante1 </variante>
<variante> B: variante2 </variante>
</question>
<question>
<variante> A: variante1 </variante>
<variante> B: variante2 </variante>
</question>
<question>
<variante> A: variante1 </variante>
<variante> B: variante2 </variante>
</question>
</questions>
A:变量1
B:变量2
A:变量1
B:变量2
A:变量1
B:变量2
整数计数器=3
因为我有三个“A”:
我想将变量用于解决方案语句:
syso(“您从“+计数器+”点获得“+到达点+”)
谢谢你的帮助 您可以执行以下操作
1) Read the XML File using say filereader class.
2) Read the content of the file as string
3) For getting the number of instance of a particular content, you can either your
String class equals method and increment a counter on every match found or use
regex (Pattern and Matcher class) to know how many times, the content occured
这个答案包含以字符串形式读入xml文件的代码,matcher.find()只会查找match的下一个实例,因此您必须循环整个过程,这很麻烦 使用DOM或SAX(取决于XML文件的大小)有很多方法可以做到这一点。这个答案涵盖了在JAVA中读/写XML的最基本的知识:如果您想使用DOM,您可以这样做:
public class blah {
public static void main(String[] args) {
File docFile = new File("foo.xml");
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
}
catch (Exception e) {
System.out.println("problem parsing the file");
System.exit(1);
}
Element root = doc.getDocumentElement();
NodeList list = root.getElementsByTagName("variante");
for (int i = 0; i < list.getLength(); i++) {
String blah = list.item(i).getTextContent();
if (blah.charAt(0) == 'A') {
System.out.println("You have " + reachedPoints + "from " + counter + "Points");
}
}
}
}
公共类废话{
公共静态void main(字符串[]args){
File docFile=新文件(“foo.xml”);
单据单据=空;
试一试{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
doc=db.parse(docFile);
}
捕获(例外e){
System.out.println(“解析文件时出现问题”);
系统出口(1);
}
元素根=doc.getDocumentElement();
NodeList list=root.getElementsByTagName(“variante”);
对于(int i=0;i
重要的部分是for循环。它存储“variante”标记的文本内容,然后检查文本内容的第一个字母是否为“A”。如果是“A”,我会让它打印出您在问题中发布的字符串。是的,这是可能的。还有问题吗?当然,读入字符串中的XML,搜索该字母,每次找到该字母时递增一个计数器。你说的“将其作为变量写入”是什么意思?此外,请展示您迄今为止所做的尝试。我已对其进行了编辑:请参见下面的解决方案句子,这样我就自动获得了最大可能点。您是否知道此文档不是有效的XML?
缺少一些结束标记。
public class blah {
public static void main(String[] args) {
File docFile = new File("foo.xml");
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
}
catch (Exception e) {
System.out.println("problem parsing the file");
System.exit(1);
}
Element root = doc.getDocumentElement();
NodeList list = root.getElementsByTagName("variante");
for (int i = 0; i < list.getLength(); i++) {
String blah = list.item(i).getTextContent();
if (blah.charAt(0) == 'A') {
System.out.println("You have " + reachedPoints + "from " + counter + "Points");
}
}
}
}