Shabana Muttaki
Shabana Muttaki

Reputation: 21

how to unmarshall an XML String with Name space to Java Object

hi I got the following response as a string when hitting a client.

I need to unmarshall it so that I can set the values in a Java object and send it back to front end. Kindly help me to convert the following xml string to jaxb object.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:ValidateAustralianAddressResponse xmlns:ns2="http://api.auspost.com.au/ValidateAustralianAddress:v1">
<Address><AddressLine>481 CHURCH ST</AddressLine><SuburbOrPlaceOrLocality>RICHMOND</SuburbOrPlaceOrLocality><StateOrTerritory>VIC</StateOrTerritory><PostCode>3121</PostCode><DeliveryPointIdentifier>55461002</DeliveryPointIdentifier><Country><CountryCode>AU</CountryCode><CountryName>Australia</CountryName></Country></Address>
<ValidAustralianAddress>true</ValidAustralianAddress>
</ns2:ValidateAustralianAddressResponse>

Upvotes: 2

Views: 2766

Answers (1)

bdoughan
bdoughan

Reputation: 149037

Metadata

Since only the root element is namespace qualified you just need to set the namespace parameter on the @XmlRootElement annotation.

@XmlRootElement(name="ValidateAustralianAddressResponse", namespace="http://api.auspost.com.au/ValidateAustralianAddress:v1")
public class ValidateAustralianAddressResponse {
}

For More Information


Converting XML to Object

You can wrap the XML String in an instance of StringReader and unmarshal that.

For More Information

Upvotes: 4

Related Questions