-2

I'm currently trying to figure out how to grab information from an XML tag and store it. To where I can sort the data by first name. So far, I've gotten to where my program would parse the data and read it. But I can't figure out how to actually store the data. I couldn't figure out how to grab information from the child tags, so all help is appreciated.

<?xml version="1.0" encoding="UTF-8"?>
<abc targetNamespace="http://www.example.com/abcd">
<abcd>
    <employeeList>
        <employee>
            <name>
                <last>Bob</last>
                <first>John</first>
            </name>
            <title>Merchant</title>
            <phone>9987583687</phone>
        </employee>

        <employee>
            <name>
                <last>Roy</last>
Bob R
  • 1

1 Answers1

2

Have you considered using JAXB (Java Architecture for Xml Binding)?

There are a range of tutorials available such as: https://docs.oracle.com/javase/tutorial/jaxb/intro/examples.html

Blaise Doughan also has some excellent resources on his blog and StackOverflow account regarding JAXB: http://blog.bdoughan.com/


As a quick introduction I made an example based of the xml you provided in the question. Assuming you have domain objects that you want to store the information from the xml too, you can use annotations to make them compatible:

import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement
public class EmployeeList {
    private List<Employee> employees = new ArrayList<>();

    public EmployeeList(){}

    public EmployeeList(List<Employee> employees){
        this.employees = employees;
    }

    public List<Employee> getEmployees(){ return this.employees; }
    public void setEmployees(List<Employee> employees){ this.employees = employees; }
}

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {
    private Name name;
    private String title, phoneNumber;

    public Employee(){ }

    public Employee(Name name, String title, String phoneNumber){
        this.name = name;
        this.title = title;
        this.phoneNumber = phoneNumber;
    }

    public Name getName(){ return this.name; }
    public String getTitle(){ return this.title; }
    public String getPhoneNumber(){ return this.phoneNumber; }

    public void setName(Name name){ this.name = name; }
    public void setTitle(String title){ this.title = title; }
    public void setPhoneNumber(String phoneNumber){ this.phoneNumber = phoneNumber; }
}

public class Name {
    private String firstName, lastName;

    public Name(){ }

    public Name(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName(){ return this.firstName; }
    public String getLastName(){ return this.lastName; }

    public void setFirstName(String firstName){ this.firstName = firstName; }
    public void setLastName(String lastName){ this.lastName = lastName; }
}

These small changes will allow you to convert your objects to and from xml

Example converting to Xml

        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(new Name("John", "Doe"), "Assistant", "123"));
        employees.add(new Employee(new Name("Jane", "Doe"), "Merchant", "456"));
        EmployeeList employeeList = new EmployeeList(employees);

        try {
            File outputFile = new File("employeeListExample.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeList.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(employeeList, outputFile);
        } catch (PropertyException exception) {
            exception.printStackTrace();
        } catch (JAXBException exception) {
            exception.printStackTrace();
        }

Which will provide the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employeeList>
    <employees>
        <name>
            <firstName>John</firstName>
            <lastName>Doe</lastName>
        </name>
        <phoneNumber>123</phoneNumber>
        <title>Assistant</title>
    </employees>
    <employees>
        <name>
            <firstName>Jane</firstName>
            <lastName>Doe</lastName>
        </name>
        <phoneNumber>456</phoneNumber>
        <title>Merchant</title>
    </employees>
</employeeList>

Note: As employee is also a root element, a single employee can also be marshalled by changing the class instance used

Example converting from Xml:

     File outputFile = new File("employeeListExample.xml");
     JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeList.class);
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
     EmployeeList list = (EmployeeList)jaxbUnmarshaller.unmarshal(outputFile);

Using the xml created above and with some small changes, we can convert the xml back into the EmployeeList that was used to create it (try/catch still required)


Using this approach you would then be able to apply your sorting to the unmarshalled list.

Whilst this answer doesn't match your xml perfectly, hopefully it will give you something to experiment with
Peter
  • 1,592
  • 13
  • 20