4

I have to work with large XML documents which often handle missing attribute data incorrectly. Rather than just omitting the attribute with the missing data, the attribute is specified with an empty string value. This causes a problem when unmarshalling, since an empty string is a value.

For example, what should be

<SOME_ELEMENT attr1="someValue"/>

Is instead

<SOME_ELEMENT attr1="someValue" attr2="" attr3=""/>

I'm trying to come up with the "right" way of gracefully handling this poorly formed XML. The goal is to treat an attribute as if it were omitted when the attribute's value is a blank string, and a blank string is never a valid value for that attribute (e.g. Integers).

I'm currently using XMLAdapters to translate these empty strings to null (as suggested in this post) but this seems like the wrong solution. Many of the attributes in the documents I'm working are already using type adapters shared from other code and there doesn't appear to be a way to specify more than one XMLAdapter for an attribute.

Is there a standard way to handle this type of situation that I've just missed?

Thanks!

EDIT

I'm looking for a solution that does not require me to edit each of the various JAXB mapped classes, since there are several dozen all with the same needs. Ideally, a solution to this problem will allow me to specify a general policy for all attributes, and allow for specific overrides as necessary.

Community
  • 1
  • 1
Terence
  • 706
  • 9
  • 22
  • What behavior do you want for ? Do you want the bound bar value to be zero or null or the empty string, or something else entirely? – David W Apr 10 '12 at 14:37
  • Also what is wrong with http://stackoverflow.com/questions/5133250/jaxb-how-to-make-jaxb-not-to-unmarshal-empty-string-to-0 ? This is a semantics problem, not an xml syntax problem. The xml is actually well-formed. I say that because it means there is not going to be an elegant technical solution because it is a business logic problem. – David W Apr 10 '12 at 14:40
  • The behavior for should be setting the value of "bar" in the associated JAXB mapped class to null, which would be treating it exactly as if the attribute was omitted. IMO, the business logic is a different thing. I would expect the code that processes the produced JAXB object tree to be responsible for the business logic (e.g. rejecting an XML document if a particular value was omitted). What I'm looking for is a blanket way to treat all empty attribute values, where empty values are never valid, as if they where omitted. Perhaps that behavior should be considered business logic? – Terence Apr 10 '12 at 19:24
  • Also, the link you posted is actually the link I referenced in my initial post and what I'm doing now. My question is wether or not that is the right solution for my situation. It seems a bit inflexible, due to the reasons I put in my first post. Based on your responses (which I appreciate!) it appears that it is the "most-right" solution considering how I'm approaching the issue. – Terence Apr 10 '12 at 19:30
  • Right...sorry about pointing you back to the link you referenced. Maybe there is a work around to transform the document first with XSL but that wouldn't really be pretty either. – David W Apr 10 '12 at 20:57

2 Answers2

0

I think the pattern the pattern you want to follow is that in this post: Customizing error handling of JAXB unmarshall process

With an element, XML can specify that it is null (as follows). With string types, the empty elements and attributes (and sometimes missing elements) can be interpreted as empty strings. Therefore, you have to treat your logic as business logic, that is handled with the afterUnmarshal event.

<foo xsi:nil="true" />
Community
  • 1
  • 1
David W
  • 945
  • 9
  • 21
  • Unfortunately, that example is element-based, not attribute based (my question is regarding attributes). Also, the afterUnmarshal method suggested in the post would not allow me to determine if a non-String value was parsed from an empty string or a legitimate value (e.g. an Integer whose value is 1). – Terence Apr 09 '12 at 14:46
  • One of my points was that only xml elements can be null, but that doesn't help you. Can you be more specific about your situation? What is the element and what is the attribute? – David W Apr 09 '12 at 23:37
  • I was a bit unclear there, sorry about that. I've updated the description – Terence Apr 10 '12 at 06:39
0

Since this has been dormant for a while now, I might as well answer the question with where we left off on it.

It looks like Skaffman's answer JAXB: how to make JAXB NOT to unmarshal empty string to 0 in the link in the description is the best option after all.

Community
  • 1
  • 1
Terence
  • 706
  • 9
  • 22