jaxb umarshalling problem: can not get the attributes on empty xml tags
I have to unmarshal a xml-soap string to 开发者_开发知识库a Java object using JAXB. The XML contains a lot of empty tags with filled attributes, for instance most information in the message is relayed as follows:
<ID code="123" codeSystem="12.12.12"/>
I am interested in the attributes.
Problem: If I inspect the object after the unmarshalling, all the empty tags (like the one above) have no representation (e.g. are null) in the Java object. Only the filled tags (e.g. 123 have been added to the Java object. Maybe this behaviour is conform xml standards, but I am still interested in the attributes. Can someone tell me if there is a way to get the attributes??
Possible workaround: to give each element a default value ("") when its null by binding it to an adapter using the bindings-file. But I only succeeded in doing this for simple-types.
Used versions: we are using the jaxb implementation in Java 1.6
Many thanks.
Wybrand.
As there is no default value for XML attributes I would implement the initialization code in the afterUnmarshal method. There you could check all attributes you are interested in and set them to a valid non-null value.
For details how to use afterUnmarshal see: How can I have JAXB call a method after it has completed unmarshalling an XML file into an object?
I solved the problem. But the problem was not JAXB.
The party which sends the xml which I have to umarshall puts a 'null namespace' in the element declaration.
Xml fragment:
The id element has in its declaration xmlns="". (I presume this is a bug) and the root tag has the declaration xmlns:ns3="urn:hl7-org:v3"
For this reason (I think) the jaxb unmarshaller does not see the id element as part of the message.
精彩评论