0

I need to know what the best solution would be to read the following xml code and place the result into objects:

             <accountInformation>
           <customField>
              <key>businessregno</key>
              <value>12345</value>
           </customField>
           <customField>
              <key>emailaddress</key>
              <value>[email protected]</value>
           </customField>
           <customField>
              <key>idnumber</key>
              <value>8601095195084</value>
           </customField>
           <customField>
              <key>initial</key>
              <value>J</value>
           </customField>
           <customField>
              <key>lastname</key>
              <value>Boshoff</value>
           </customField>
           <customField>
              <key>licensetype</key>
              <value>07</value>
           </customField>
           <customField>
              <key>licensetypedescription</key>
              <value>Normal</value>
           </customField>
           <customField>
              <key>loaduserid</key>
              <value>12345</value>
           </customField>
           <customField>
              <key>passportno</key>
              <value>1234512345</value>
           </customField>
           <customField>
              <key>title</key>
              <value>Mr</value>
           </customField>
           <customField>
              <key>validationrefno</key>
              <value>0</value>
           </customField>
           <customField>
              <key>accountnumber</key>
              <value>608806709</value>
           </customField>
           <customField>
              <key>balance</key>
              <value>500</value>
           </customField>
           <customField>
              <key>contactno</key>
              <value>0846769478</value>
           </customField>
           <customField>
              <key>landlinecontactno</key>
              <value>0218865557</value>
           </customField>
           <customField>
              <key>physicaladdress</key>
              <value>SUITE 4,OU KOLLEGE GEBOU,STELLIES,WESTERN PROVINCE,5600,35,CHURCH STREET,</value>
           </customField>
           <customField>
              <key>validity</key>
              <value>true</value>
           </customField>
        </accountInformation>

The problem I have tried a hash map, but all the elements have the same name so I have a issue populating it into a list.

What would be the best way for me to write it into java objects?

Thank you for the help.

4
  • I'll go with list of string array's. Commented Aug 24, 2015 at 11:34
  • I don't see all elements having the same name.. there is a key tag and thus key should be unique. Make sure it is.. anyhow what about list of string array? Commented Aug 24, 2015 at 11:34
  • 1
    I would use the XStream library . Commented Aug 24, 2015 at 11:35
  • I would recommend using JAXB unmarshalling. Take a look here: Convert XML to Java object using JAXB unmarshal Commented Aug 24, 2015 at 13:05

2 Answers 2

1

I'm doing something similar and found the w3c DOM is pretty decent.

File xmlFile = new File(path);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(
        "http://apache.org/xml/features/nonvalidating/load-external-dtd",
        false);
DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.parse(xmlFile); //or parse the String

document.getDocumentElement().normalize();

So now you have the xml file as a Document object.

Map<String, String> map = new HashMap<String, String>();
NodeList nodelist = document.getElementsByTagName("customField")

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

    String key = element.getAttributes("key");
    String value = element.getAttributes("value");

    map.put(key,value);
}

And now you have a map of key/values.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are working on spring create bean for org.springframework.oxm.jaxb.Jaxb2Marshaller by setting property classesToBeBound to pojo class.

1 Comment

it's a good idea to include some example code with your answer, so the OP can learn from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.