I have an xml with an envelope, in the envelope a header, in the header there is an element called Period with a Startdate and an Enddate. But in some cases they're called StartDate and EndDate. Is there some way of saying to JAXB to look for both when unmarshalling?
I already have a class Envelope with in there bunch of classes for all the element parents I need with each having child elements if needed. One of them is the class Period with in there the two @XmlElement public String ***date
.
I parse this with unmarshall and then access the data in my InsertProcessor to put them in queries and save it to a DB.
I tried using @XmlElements({})
but that didn't work:
@XmlElements({
@XmlElement(name = "Startdate", type = Startdate.class),
@XmlElement(name = "StartDate", type = StartDate.class)
})
private DateStart startdate;
@XmlElements({
@XmlElement(name = "Enddate", type = Enddate.class),
@XmlElement(name = "EndDate", type = EndDate.class)
})
private DateEnd enddate;
public Startdatum getStartdate() {
return startdate;
}
public Stopdatum getEnddate() {
return enddate;
}
abstract class DateStart{}
private class Startdate extends DateStart{}
private class StartDate extends DateStart{}
abstract class DateEnd{}
private class Enddate extends DateEnd{}
private class EndDate extends DateEnd{}
trying to access it in InsertProcessor.java with env.Header.Period.getStartdate();
IDE responding with Incompatible types. Required String, Found Parser.Startdatum
Sample.xml
<envelope>
<data>
<msgcode>441</msgcode>
<msgdate>201903151120000</msgdate>
</data>
<Message>
<Header>
<Period>
<Startdate>2018-05-01</Startdate> <!-- can also be StartDate -->
<Enddate>2018-05-31</Enddate> <!-- can also be EndDate -->
</Period>
</Header>
</Message>
</envelope>
Parser.java
public class Parser {
public static Envelope parse (String xml) throws JAXBException, SAXException {
JAXBContext jc;
jc = JAXBContent.newInstance(Envelope.class)
Unmarshaller u = jc.createUnmarshaller();
...
InputSource is = new InputSource(new StringReader(xml));
return (Envelope) u.unmarshal(new SAXSource(inFiler, inputSource));
}
@XmlRootElement(name="Envelope")
public static class Envelope {
@XmlElement(name = "Data")
public Data Data;
@XmlElement(name = "Message")
public Message Message;
}
...
public static class Message {
@XmlElement(name = "Header")
public Header Header;
...
}
public static class Header {
@XmlElement(name = "Period")
public Period Period;
...
}
public static class Period {
@XmlElement(name = "Startdate")
public String Startdate;
@XmlElement(name = "Enddate")
public String Enddate;
}
...
}
InsertProcessor.java:
public class InsertProcessor {
public void process(String xml) throws Exception {
Parser.Envelope env = Parser.parse(xml);
String id = env.Header.Identification;
...
String Startdate = env.Header.Period.Startdate;
String Enddate = env.Header.Period.Enddate;
try (Connection c = Db.getConnection()) {
PreparedStatement ps = c.prepareStatement("insert into...");
ps.setString(1, id)
...
ps.setString(9, Startdate);
ps.setString(10, Enddate);
ps.execute();
}
}
So I need to get either Start/Enddate or Start/EndDate. Right now I'm just getting Start/Enddate