0

I already have the implementation to post the xml data to my controller but I am having some issues with the data I am trying to read due the XML format I given work with.

My Controller:

@RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE}, produces = {MediaType.APPLICATION_XML_VALUE} )
    public ResponseEntity<?>  SlaDetails(@RequestBody AgentSla[] task_sla) {
        MessageDto messageDto = new MessageDto();
        for (AgentSla next: task_sla){
            System.out.println(next.getStage());
        }
        messageDto.setMsg("ok");
        return ResponseEntity.status(HttpStatus.CREATED).body(messageDto);
    }

My Entity:

    @XmlRootElement(name="task_sla")
    public class AgentSla {

    private Long id;
    private String stage;
    private String timezone;

    //getters and setters

XML doc sample:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
<task_sla>
<stage>boo</stage>
<timezone display_value="IN">12345</timezone>
</task_sla>

<task_sla>
<stage>foo</stage>
<timezone display_value="SR">12345</timezone>
</task_sla>
</xml>

The Issue: I can read any value of the XML "subchilds" i.e "<stage>boo</stage>" or </timezone> the problem I have is on the "<timezone display_value="IN">12345</timezone>". On this child it reads only the value "12345" but I also need the "display_value="SR" " value. Is it possible to read that value or...should I just give up.

user2342259
  • 345
  • 2
  • 9
  • 27

1 Answers1

0

you should make a new class for timezone field and use @XmlAttribute

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "timezone")
public class Timezone {
    @XmlAttribute(name = "display_value")
    private String displayValue;

    @XmlValue
    private String timezoneValue;

    // write your getters and setters
}

Then use Timezone class in your

@XmlRootElement(name="task_sla")
public class AgentSla {
    private Long id;
    private String stage;
    private Timezone timezone;
    // getters and setters
}

see here:

How can I add xml attributes to jaxb annotated class XmlElementWrapper?

XML element with attribute and content using JAXB

shawn
  • 4,305
  • 1
  • 17
  • 25