Reputation: 3156
I am using JAXB to convert xml to object. And I am creating class manually because WS vendor does not provide us schema.
XML is:-
<com:DayAndTimeAvailability>
<com:DayTypes>
<com:MondayToSunday />
</com:DayTypes>
<com:OpeningHours>
<com:TwentyFourHours />
</com:OpeningHours>
</com:DayAndTimeAvailability>
I am confuse in "DayTypes" element. It can have values
<com:MondayToFriday/>,<com:Weekend/>,<com:MondayToSunday/>, <com:Monday/>,<com:Tuesday/>, <com:Wednesday/>,<com:Thursday/>, <com:Friday/>,<com:Saturday/>, <com:Sunday/>
.
I want a String variable which have value based on upper mention elements. like if i got <com:MondayToFriday/>
in xml then i need to save value Monday - Friday 24 Hours a Day in a string.
Can anyone help me?
Upvotes: 0
Views: 1597
Reputation: 114
If you have xml, then there is no need to create classes manually. Create xsd for this xml, then using xjc command you can generate POJO's. To map your xml to object use this,
JAXBContext context = JAXBContext.newInstance(YourMainClass.class);
Unmarshaller u = context.createUnmarshaller();
yourMainClassObject= (YourMainClass) u.unmarshal(new StringReader(xml));
Upvotes: 2
Reputation: 3156
I did like this
@XmlRootElement(name="DayTypes")
public static class DayTypes{
private ElementNSImpl element;
private String value;
@XmlAnyElement
public ElementNSImpl getElement() {
return element;
}
public void setElement(ElementNSImpl element) {
String nodeName = element.getNodeName();
switch (nodeName) {
case "com:MondayToSunday":
setValue("Monday - Sunday 24 Hours a Day");
break;
case "com:MondayToFriday":
setValue("Monday - Friday 24 Hours a Day");
break;
default:
setValue(nodeName);
break;
}
this.element = element;
}
@XmlTransient
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
But i do not know that it is correct way or not.
Upvotes: 1