【问题标题】:Java: Count a special letter or number from a .XMLJava:计算 .XML 中的特殊字母或数字
【发布时间】:2014-02-18 13:27:10
【问题描述】:

我想问我是否有可能计算一个特殊的字母或数字,例如:

“A:”

来自 .xml 文件? 并将其写为变量?

例如:

foo.xml:

<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>

int 计数器 = 3

因为我有三个“A:”

我想将变量用于解决句子:。

syso("你有" +达到的点数+"来自"+计数器+"点数");

感谢您的帮助!

【问题讨论】:

  • 是的,这是可能的。还有问题吗?
  • 当然,以字符串形式读取 XML,搜索该字母并在每次找到该字母时增加一个计数器。
  • "把它写成变量"是什么意思?另外,请展示您迄今为止尝试过的内容
  • 我已对其进行了编辑:请参阅下面的解决方案语句,以便我自动获得最大可能积分
  • 您知道此文档不是有效的 XML 吗? &lt;variante&gt; 缺少一些结束标签。

标签: java xml file variables counter


【解决方案1】:
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new FileReader(PATH_TO_XML_FILE));
    String sCurrentLine = null;

    while ((sCurrentLine = br.readLine()) != null) {
        sb.append(sCurrentLine);
    }

    Pattern pattern = Pattern.compile("A:");
    Matcher matcher = pattern.matcher(sb);
    int count = 0;
    while (matcher.find()){
        count++;
    }

    System.out.println(count);

这个答案还包含以字符串形式读取 xml 文件的代码,matcher.find() 仅查找匹配的下一个实例,因此您必须循环浏览整个内容,这很烦人。

【讨论】:

    【解决方案2】:

    你可以做以下事情

    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
    

    【讨论】:

      【解决方案3】:

      有很多方法可以使用 DOM 或 SAX(取决于 XML 文件的大小)。这个答案涵盖了在 JAVA 中读取/写入 XML 的最基本知识:Java: How to read and write xml files?

      【讨论】:

        【解决方案4】:

        如果你想使用 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");
                            }
                    }
            }
            }
        

        重要的部分是 for 循环。它存储“variante”标签的文本内容,然后检查文本内容的第一个字母是否为“A”。如果它是“A”,那么我让它打印出您在问题中发布的字符串。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-08
          • 1970-01-01
          • 2013-05-31
          • 1970-01-01
          • 1970-01-01
          • 2021-05-10
          • 1970-01-01
          • 2018-12-13
          相关资源
          最近更新 更多