-1

i wanted to ask if it's possible that i count a special letter or number for example:

"A:"

from a .xml file ? And write this as a Variable ?

For example:

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 counter = 3

Because i have three "A:"

I want to use the Variable for an solution sentence:.

syso("You have " + reachedPoints + "from " + counter + "Points");

Thanks for your help !

Atlantikdiver
  • 142
  • 1
  • 15

4 Answers4

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

This answer contains code to read in the xml file as a String also, matcher.find() only finds next instance of match so you have to cycle through the whole thing, annoyingly.

user898465
  • 944
  • 1
  • 12
  • 23
0

You can do the following thing

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
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
0

There are a lot of ways to do this with either DOM or SAX (depending on the size of your XML file). This answer covers the most basics on reading / writing to XML in JAVA: Java: How to read and write xml files?

Community
  • 1
  • 1
BadSkillz
  • 1,993
  • 19
  • 37
0

If you wanted to use the DOM you could do something like this:

    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");
                    }
            }
    }
    }

The important part is the for loop. That stores the text content of your "variante" tags and then checks if the first letter of the text content is "A". If it is "A", then I have it printing out the string you posted in your question.