Search code examples
javaxmldom4j

How to properly handle null values when creating an xml document with dom4j


I'm using dom4j to create XML documents from entities.

I'm doing something like:

Entity entity = getSomeEntity();
element.addElement("foo").addText(entity.getSomeField());
element.addElement("bar").addText(entity.getAnotherField());    
...

However, addText() does not allow null values and throws an IllegalArgumentException when doing so.

Is there another way of creating an xml document without having to null check every field of the entity? It can get quite messy with large entities.


Solution

  • Write a utility method. But you have to decide how to represent a null value: an empty tag or an absent tag.

    Did you consider JAXB?