1

I am a novice in SAXParser. I don't know is it possible to parse complex object with SAXParser. I have a class which contain Item list. And my response xml is like that :

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetPaymentServiceInfoResponse xmlns="http://quetzalcoatlus/EpulPaymentService"><GetPaymentServiceInfoResult><ResultCodes>OK</ResultCodes><Description>User is temporary blocked</Description><Items><Item><Amount>122</Amount></Item><Item><Amount>23232</Amount></Item></Items></GetPaymentServiceInfoResult></GetPaymentServiceInfoResponse></s:Body></s:Envelope>

And my POJO class is like following:

@XmlRootElement(name = "PGResponse")
 public class CheckAbonSuccessResponseModel {

private String message;
private String message_code;
private BigDecimal amount;
private String invoiceCode;
private String operationCode;
private List<Item> items;

@XmlElement(name = "message")
public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

@XmlElement(name = "message_code")
public String getMessage_code() {
    return message_code;
}

public void setMessage_code(String message_code) {
    this.message_code = message_code;
}

@XmlElement(name = "amount")
public BigDecimal getAmount() {
    return amount;
}

public void setAmount(BigDecimal amount) {
    this.amount = amount;
}

@XmlElement(name = "invoice_code")
public String getInvoiceCode() {
    return invoiceCode;
}

public void setInvoiceCode(String invoiceCode) {
    this.invoiceCode = invoiceCode;
}

@XmlElement(name = "operation_code")
public String getOperationCode() {
    return operationCode;
}

public void setOperationCode(String operationCode) {
    this.operationCode = operationCode;
}

@XmlElement(name = "items")
public List<Item> getItems() {
    return items;
}

public void setItems(List<Item> items) {
    this.items = items;
}

@XmlRootElement(name = "item")
public static class Item {
    private String label;
    private String value;

    @XmlElement(name = "label")
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    @XmlElement(name = "value")
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
 }
  }

How I can parse my xml string to CheckAbonSuccessResponseModel. Is it possible or not? I was trying but it shows just amount inside Result element.I need just know how I must write DefaultHandler class. Thanks in advance.

Resul Rzaeeff
  • 448
  • 4
  • 14
  • 30
  • Your `@XmlRootElement` does not match the `GetPaymentServiceInfoResult` element which appears in the XML. – lexicore Mar 07 '18 at 08:43
  • Please see this article: [Handle the Middle of a XML Document with JAXB and StAX](https://dzone.com/articles/handle-middle-xml-document) – lexicore Mar 07 '18 at 08:44
  • Thank you for answers. I need populate CheckAbonSuccessResponseModel inside my handler class. How i can do it ? – Resul Rzaeeff Mar 07 '18 at 08:46
  • I am confused with startElement , endElement ,characters methods of handler class. – Resul Rzaeeff Mar 07 '18 at 08:49
  • 1
    Why do you need to do it with a SAX parser? I've posted a link on a JAXB/StAX approach which is much simpler. You don't need to deal with `startElement` etc., JAXB unmarshaller will do it for you. Please do read the article. – lexicore Mar 07 '18 at 09:10
  • I want to get values from string xml response and set to my model class and marshal it to string and return. – Resul Rzaeeff Mar 07 '18 at 10:24
  • JAXB unmarshaller will do this for you. No need to get messy with SAX: – lexicore Mar 07 '18 at 10:24
  • my string xml and model class are different. – Resul Rzaeeff Mar 07 '18 at 10:26
  • Then make a matching model class, unmarshal to it and then copy to your "normal" model class. That will be was easier than SAX. I'm not sure why you persist on it. – lexicore Mar 07 '18 at 10:28

0 Answers0