I am trying to parse the following xml. I can access the WeekNumber easily but cannot access the children for EmployeeRatesLevelA and EmployeeRatesLevelB. The goal is to sav
As mentioned in the comments, please show us your Java code. You may also want to consider looking at JAXB - the Java Architecture for XML Binding. This is specifically geared towards representing XML as Java objects. It may not be feasible for your solution for whatever reason, but definitely take a look:
http://jaxb.java.net/tutorial/
DataSet
The following domain object below is what you described in your question:
The goal is to save these to a class, DataSet with fields WeekNumber and ArrayLists, EmployeeRatesLevelA and EmployeeRatesLevelB.
package forum8345529;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="DataSet")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSet {
@XmlElement(name="WeekNumber")
private int weekNumber;
@XmlElementWrapper(name="EmployeeRatesLevelA")
@XmlElement(name="Rate")
private List employeeRatesLevelA;
@XmlElementWrapper(name="EmployeeRatesLevelB")
@XmlElement(name="Rate")
private List employeeRatesLevelB;
}
Demo
The following code demonstrates how to use the JAXB runtime to convert your XML to/from your domain objects:
package forum8345529;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception{
JAXBContext jc = JAXBContext.newInstance(DataSet.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum8345529/input.xml");
DataSet dataSet = (DataSet) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(dataSet, System.out);
}
}