0

For a project, I'm forced to use certain webservices. I created a small java project that does nothing but open the correct httpconnection, writes input and gets the output. This output is generally xml text or json text. I'm now wondering on how to convert this text to java objects.

I can either do it manually by using the following OOP scheme: When calling a webservice, if it has output, a "handler" class must be supplied. this handler class must conform to this interface:

public interface Handler<T>
{
    public T handle(Byte[] webserviceOutput)
}

And a helper class for specific XML cases:

public abstract class XMLHandler<T> implements handler<T>
{
    public T handle(Byte[] webserviceOutput)
    {
         Document xmldoc = convertByteArrayToDocument();
         return parseXmlDoc(xmlDoc);
    }

    public abstract T parseXmlDoc(Document xmlDoc);
}

I like how clean this interface is, but the actual implementations are very unclean and all involved parsing the entire input, well, line by line and calling alot of setters manually (in other words: no reflection is used).

On the other hand, libraries exist that convert json and xml into existing java objects. Their major downside is they are very unflexible because the names of fields must match exactly to those in the xml. This is a burden when dealing with crazy dba's who abbreviate all their fields into gibberish (for example field 'apb' which should be named 'adjustablePersonBudget'). another problem is the autio-conversions tend to be verbose. For example, the xml

<books>
    <book></book>
    <book></book>
    ...
</books>

results in java in:

rootObject.getBooks().getBookList() instead of just rootObject.getBookList()

The obvious advantage of using libraries is you don't converter classes and just reflections.

What are you experiences and thoughts on these two approaches for enterprise software?

user1884155
  • 3,616
  • 4
  • 55
  • 108
  • JAXB can make it simpler, if you like that sort of thing. There's nothing wrong with your approach. – duffymo Aug 19 '15 at 10:57
  • And regarding JSON, if you use Jackson for example, you can map different names to your fields with [@JsonProperty](https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations), eg: `@JsonProperty("al_count") private long numberOfAlarms;` – Morfic Aug 19 '15 at 11:01
  • Don't use `Byte[]`: use `byte[]` instead. – Olivier Grégoire Aug 19 '15 at 13:53

2 Answers2

0

If you have an XML schema , JAXB is nice - comes as part of the JDK. Generate java classes by running e.g. xjc -p foo myschema.xsd

To read an XML file and get back an object (from classes generated by the xjc tool):

JAXBContext context = JAXBContext.newInstance(FooObj.class);
Unmarshaller unMarshaller = context.createUnmarshaller();
FooObj param = (FooObj) unMarshaller.unmarshal(new FileInputStream("Foo.xml"));

You can do similar things if you only want parts of an XML document converted to an object, you should e.g. be able to give JAXB part of a DOM document, instead of a whole file as done above. you can check here Converting XML to Java objects

Community
  • 1
  • 1
Rama Krishna
  • 120
  • 8
0

One question to ask yourself is: do you really need Java at all? The so-called "XRX" philosophy is to keep the data in the XML model end-to-end, by using languages and tools such as XQuery, XSLT, and XForms. As you're discovering, converting data from one format to another can end up being half the work of the project, and it's well worth avoiding it if you can.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164