-1

I have a chunk of XML, that is turned into a String via substring. I need to compare some data within it, so need to turn it into the HashMap. This is the XML:

title="Language Dependence" totalvotes="32">
            
        <results>        
                    <result level="1" value="No necessary in-game text" numvotes="0" />
                    <result level="2" value="Some necessary text - easily memorized or small crib sheet" numvotes="7" />
                    <result level="3" value="Moderate in-game text - needs crib sheet or paste ups" numvotes="22" />
                    <result level="4" value="Extensive use of text - massive conversion needed to be playable" numvotes="2" />
                    <result level="5" value="Unplayable in another language" numvotes="1" />
                </results>                    
    </poll>

I have written the HashMap Code, but it does not seem to work. What am I doing wrong?

Response response = RestAssured.get(api address);

        String code = response.asString();

        String partialCode = code.substring(15967, 16535);

        String partialCodeAm = partialCode.replaceAll("/>", "");
        String partialCodeAp = partialCodeAm.replaceAll("<", "");
        String partialCodeAf = partialCodeAp.replaceAll("\"", "");
        String partialCodeAd = partialCodeAf.replaceAll("results>", "");
        String partialCodeAu = partialCodeAd.replaceAll(">", "");
        String partialCodeAc = partialCodeAu.replaceAll("/results>", "");
        String partialCodeAk = partialCodeAc.replaceAll("/", "");

        String[] parts = partialCodeAk.split(",");

        Map<String, String> map = new HashMap<>();

        for (String part : parts) {

            String[] voteData = part.split(":");

            String level = voteData[0].trim();
            String numvotes = voteData[0].trim();

            map.put(level, numvotes);
        }
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Can you add the missing parts of your XML? I am a little confused with your code as you split at `,`, but your XML does not contain a single `,` character. Is [this](https://stackoverflow.com/questions/27547292/how-to-parse-xml-to-hashmap) answering your question? – JANO Aug 06 '22 at 09:37
  • 4
    I'm not a little confused, I'm totally confused. The code seems to bear no relationship to the input at all. – Michael Kay Aug 06 '22 at 09:58
  • I have added the full code – Mari Jeparska Aug 06 '22 at 11:05
  • "What am I doing wrong?" hard to tell because your code still doesn't seem to be related to XML as you are trying to split on `:` which doesn't seem to appear in your input. Anyway it would be *much* easier to write clean code with XML parser. But to suggest proper solution we need to know what results you expect. – Pshemo Aug 06 '22 at 11:16
  • [It is completely wrong](https://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg) to use regular expressions to parse XML. As Pshemo said, use an XML parser. There’s two that [come with Java SE.](https://docs.oracle.com/en/java/javase/18/docs/api/java.xml/javax/xml/parsers/package-summary.html) – VGR Aug 06 '22 at 15:02

1 Answers1

2

To parse XML use XML parser. One of easier but yet powerful ones is Jsoup which allows us to write code like

String XMLString = """
        <results>
            <result level="1" value="No necessary in-game text" numvotes="0" />
            <result level="2" value="Some necessary text - easily memorized or small crib sheet" numvotes="7" />
            <result level="3" value="Moderate in-game text - needs crib sheet or paste ups" numvotes="22" />
            <result level="4" value="Extensive use of text - massive conversion needed to be playable" numvotes="2" />
            <result level="5" value="Unplayable in another language" numvotes="1" />
        </results>
        """;

Map<String, String> map = new HashMap<>();
Elements allResultElements = Jsoup.parse(XMLString, Parser.xmlParser()).select("result");
for (Element result : allResultElements){
    map.put(result.attr("level"), result.attr("numvotes"));
}

System.out.println(map);

Output: {1=0, 2=7, 3=22, 4=2, 5=1}

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Isn’t Jsoup for HTML? I would use javax.xml.xpath for this. – VGR Aug 06 '22 at 14:59
  • 1
    @VGR `Jsoup.parse(XMLString, Parser.xmlParser())` allows us to chose XML variant of parsing. Anyway main idea behind this answer is to show that parser is easier way to handle XML than our own string manipulation. – Pshemo Aug 06 '22 at 16:24