0

I have the following XML which I want to unmarshall using JAXB:

<?xml version="1.0" encoding="UTF-8"?>
<questions>
    <question id="1">
        <text>What is the capital of Germany?</text>
        <correctAnswer>2</correctAnswer>
        <answers>
            <answer id="1">
                <text>Munich</text>
            </answer>
            <answer id="2">
                <text>Berlin</text>
            </answer>
        </answers>
    </question>
    <question id="2">
        <text>What is the capital of France?</text>
        <correctAnswer>1</correctAnswer>
        <answers>
            <answer id="1">
                <text>Paris</text>
            </answer>
            <answer id="2">
                <text>Marseille</text>
            </answer>
        </answers>
    </question>
</questions>

I want to create a HashMap:

HashMap<String, Question>

from which I can get the questions by their ID.

Each question object should also have a Hashmap:

HashMap<String, Answer>

from which I can get the answers by their ID.

I started off using the code from this thread: JAXB unmarshal XML elements to HashMap

Now I have two problems / questions:

  1. The accepted answer in that thread suggests using "@XmlPath(".")", but for that I'm supposed to import an eclipse plugin:

    import org.eclipse.persistence.oxm.annotations.XmlPath;
    

    But I'm using IntelliJ, and I don't know which alternative import to use to get this working.

  2. Even if I get the proposed solution "@XmlPath(".")" working, I still don't know how to implement the HashMap property within the question object (containing the answers).

David
  • 105
  • 1
  • 8
  • @tima: thanks for the suggestion, but the thread which I mentioned in my question (https://stackoverflow.com/questions/20538149/jaxb-unmarshal-xml-elements-to-hashmap) and which is the basis for my implementation already uses the XmlAdapter. The two problems remain: 1. XmlPath not working because of a "missing" import and 2. how to get another "level" of HashMap within the objects of the first HashMap). – David Aug 24 '17 at 18:55
  • I'm not sure why you need the `XmlPath` annotation, the XmlAdapter seems to work pretty well without it. The duplicate has several links with working examples. – tima Aug 24 '17 at 19:02
  • I'll just put the link here instead of flagging: https://stackoverflow.com/questions/9275805/how-to-marshall-unmarshall-map-using-jaxb – tima Aug 24 '17 at 19:03

0 Answers0