-1

I try to get "Value" String which have the largest value of NumVotes. After that I would like do to simple asserion (I know how to do it), but I have problems how to get this "Value". I don't have idea and knowledge about it.

public class DomParserDemo {

    public static void main(String[] args) {

        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader("<poll name=\"language_dependence\" title=\"Language Dependence\" totalvotes=\"170\"><results><result level=\"1\" value=\"No necessary in-game text\" numvotes=\"168\"/><result level=\"2\" value=\"Some necessary text - easily memorized or small crib sheet\" numvotes=\"0\"/><result level=\"3\" value=\"Moderate in-game text - needs crib sheet or paste ups\" numvotes=\"0\"/><result level=\"4\" value=\"Extensive use of text - massive conversion needed to be playable\" numvotes=\"0\"/><result level=\"5\" value=\"Unplayable in another language\" numvotes=\"2\"/></results></poll>"));
            Document doc = dbBuilder.parse(is);
            doc.getDocumentElement().normalize();
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nodeList = doc.getElementsByTagName("result");

            for (int i = 0; i < nodeList.getLength(); i++) {
                Element element = (Element) nodeList.item(i);

                System.out.println("Level = "+ element.getAttribute("level")+", "+
                        "Value = "+element.getAttribute("value")+", "+
                        "NumVotes = "+element.getAttribute("numvotes"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Current output:

      Root element :poll
Level = 1, Value = No necessary in-game text, NumVotes = 168
Level = 2, Value = Some necessary text - easily memorized or small crib sheet, NumVotes = 0
Level = 3, Value = Moderate in-game text - needs crib sheet or paste ups, NumVotes = 0
Level = 4, Value = Extensive use of text - massive conversion needed to be playable, NumVotes = 0
Level = 5, Value = Unplayable in another language, NumVotes = 2
  • Are you truly asking how to find the `Element` object with the largest `numvotes` attribute, so you can get its `value` attribute? Please do some **research**, e.g. a *web search* for [`java find largest`](https://www.google.com/search?q=java+find+largest) will give you tons of examples for doing that. – Andreas Jul 16 '19 at 20:59

1 Answers1

0

You are already iterating all the elements. Finding maximum by one of the element attributes can be achieved by just keep track of maximum so far using a variable and compare that against each value. Your for loop is modified a bit:

String targetValue = "";
int maxNumVotes = 0;
for (int i = 0; i < nodeList.getLength(); i++) {
    Element element = (Element) nodeList.item(i);
    int numVotes = Integer.parseInt(element.getAttribute("numvotes"));
    if (numVotes > maxNumVotes) {
        maxNumVotes = numVotes;
        targetValue = element.getAttribute("value");
    }
}
System.out.println("Value: " + targetValue + " NumVotes: " + maxNumVotes);

However, if you want to sort elements by the number of votes, add elements to a list and use a comparator to sort by the number of votes or any other field.

fiveelements
  • 3,649
  • 1
  • 17
  • 16
  • Yours solution looks very nice ! I forget ... I will need to parse it from bigger XML - https://www.boardgamegeek.com/xmlapi/boardgame/148228/splendor. – popMichael Jul 16 '19 at 21:54
  • Yes, you may parse a bigger XML and apply your business logic on the data read. However, if you have got the answer you are looking at please mark this as the correct answer. Otherwise, please explain what exactly you are looking at. – fiveelements Jul 16 '19 at 22:13
  • I have a problem with adapt this code for this bigger XML boardgamegeek.com/xmlapi/boardgame/148228/splendor. – popMichael Jul 16 '19 at 22:24
  • Please be specific and explain what exactly you want to do with this XML data. – fiveelements Jul 16 '19 at 22:59
  • The same as above I would like to do, first time I tried with shorter XML (please have a look above). Now, I try to work with oryginal XML (longer) from here boardgamegeek.com/xmlapi/boardgame/148228/splendor – popMichael Jul 16 '19 at 23:09