Reputation: 149
I am building an APP that uses XML requests and responses to communicate with a server via HTTP POST. Due to copyright restriction I cannot use SIMPLE XML to do (at least partially) what JAXB would normally do, and from all the information I have JAXB cannot be used on Android.
I did the job using XmlSerializer to build the request and XMLPullParser for parsing the response but I don`t like how the code looks like and I was not able to find any standard equivalent for JAXB in the Android SDK.
Is there any SDK component that I can use to transform the XSD schema files to Java classes and then use marshaller/unmarshaller to/from a XML file or I am stuck with using XmlSerializer/XMLPullParser?
Upvotes: 2
Views: 2197
Reputation: 43
I had the same question -- and figured it out. Using jackson-module-jaxb-annotations and XmlToJson :
public static String jaxbObjectToXML(SomeClass object)
{
JaxbAnnotationModule module = new JaxbAnnotationModule();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
try {
JsonToXml jsonToXml = new JsonToXml.Builder(
objectMapper.writeValueAsString(object)).build();
return jsonToXml.toString();
}catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
Dependencies:
dependencies {
...
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
implementation 'com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.4.0'
implementation 'com.github.smart-fun:XmlToJson:1.5.1'
}
Upvotes: 0
Reputation: 43709
See some previous posts on the topic:
I'd rather try SimpleXML.
Upvotes: 0
Reputation: 1007544
Is there any SDK component that I can use to transform the XSD schema files to Java classes
No.
I am stuck with using XmlSerializer/XMLPullParser?
You are welcome to use DOM or SAX instead of XmlPullParser
, just as you can in classic Java.
There are also several XML parsing libraries for Android, beyond just SimpleXML.
Upvotes: 2