Soap: How to validate empty elements - java

How is it possible to validate that an element in a Soap-Request must exist and must contain a value?
I have a webservice that receives Soap requests which contain mandatory-values. These values must not be empty and must exist in the xml-document.
If I use
#XmlElement(nillable=false, required=true)
to achieve that, I can validate that the element must exist and that it may not be empty if the element has the attribute
xsi:nil="true"
But what if the client does not add the nil-attribute and just send an empty element like
<element/>
How can I define that this is forbidden? In other words: How can I define that only elements like
<element>value</element>
are allowed?
Thanks!

JAXB facets claims to that
the generated WSDL file describing the service interface will contain
an XSD declaration
that was defined with annotations.
If you're using code first approach, you could generate WSDL and deploy your WS, instead of leaving WSLD generation for JAX-WS runtime.

Related

how to skip few element validations in xsd in java

I am trying to validate an xml against an xsd in java.
In XSD one of the field(tax_id) is defined as manadatory element.
But in my scenario I pass an xml to another component, that component fills the manadatory
field(tax_id).
Before sending that xml to the next component, I have to validate that xml against the xsd.
As, in that XSD element tax_id is defined as mandatory element, I get exception for not filling mandatory element (tax_id).
I can create a new xsd by making tax_id as optional field, but with this we would be having 2 xsds.
Is there any way to skip/ignore few elements while validating in java?
In general, no. The purpose of the XSD is to specify the rules that the document must meet, in order to be valid. You can't ignore or skip some of those rules. If you could, you'd probably have other problems. For example, if required elements were really optional (or could be) then technically any element that was supposed to contain a bunch of other elements could be empty (and still valid) under that more lax validation.
In your situation, you probably have two options:
Change your workflow - make sure the first component populates the XML with the empty tax_id. Then it will validate.
Introduce a second schema - one earlier in the "pipeline" of processing, that doesn't require tax_id. Then validate against that.

Parse xml with same properties but different parent elements using JAXB

I'm using JAXB to parse some xml provided by a separate service. They provide this xml as different elements but I treat them the same. I.E.
<ElementA>
<name>foo</name>
<type>bar</type>
</ElementA>
<ElementB>
<name>foo1</name>
<type>bar1</type>
</ElementB>
This is how in the data but when I parse/unmarshall it with JAXB I want it to know that ElementA and ElementB are both just instances of the Element class and unmarshall it as such. Currently I am doing this by using the XmlRegistry to say that ElementA is an Element and ElementB is an Element.
However at some point we might receive ElementC and ElementD so I would like a way for JAXB to know those are also just Element without having to add an entry to the XmlRegistry everytime.

How to null check and index check these JAXB types of classes

I have been provided an XSD file to unmarshall the XML returned from an external web service.
I'm able to unmarshall the XML I receive into the JAXB classes. But due to the depth of the structure of the JAXB classes generated I'm not sure on how to null check the code while retrieving values.
Example to retrieve a students name, the code is as follows -
jaxbResponse.getStudentDetails().get(0).getStudent().get(0).getName().get(0).getGivenName();
It gets really messy if I have to null check and index check the lists this code returns.
This is just an example, I need to get around 50 parameters with code similar to the above code. I have no idea when I'm going to get a NullPointerException or an IndexOutofBoundsException.
I'm new to working with JaxB classes, I need some help.
Thanks in advance.
The pattern of your example looks like a List generated from a simple <xs:element minOccurs="{something}" maxOccurs="unbounded"> in which case you should never get a null List (though you may get an empty list if minOccurs is 0), and you won't get null values out of the list either. You'll only have to worry about nulls if you have an #XmlElementWrapper or your schema declares elements as nillable.
I can't be more specific without seeing (at least an extract from) your schema and/or generated classes.

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.

Duplicate namespace declarations in JAXB generated XML

I am using JAXB to generate XML from Java objects, it's a realtime, quite high message rate application and works fine most of the time. However occassionally and without any obvious clues as to why, I am getting duplicate namespace declarations in the generated XML. eg:
<UpdateRequest xmlns="http://xml.mycomp.com/ns/myservice"
xmlns="http://xml.mycomp.com/ns/myservice">
<field1>value</field1>
...
</UpdateRequest>
Has anyone seen this behaviour before?
Check if the xsd code of this class allow the creation of more than 1 instance of the repeated attribute. if so, you can avoid this repetitions setting the number of instances of the xmlns attribute for each UpdateRequest object.
If the problem is your code (maybe there is being created this attribute twice) and you have limited the number of instances of the attribute (as i said above), the program will show an error at runtime complaining that you are trying to insert an attribute already defined.
A solution might be available at this link.
here's the relevant section quoted verbatim from the above link that may be relevant for you:
Similar explicit inclusion of a schema
type in an instance document's element
occurs if you instantiate a JAXB
element using an object of some
(abstract) XML schema base type so
that the element would have the
element tag of the base type.
Second, avoid xs:anySimpleType since
this will also create multiple
references to the namespaces bound to
xsi and xs, and type attributes
containing the actual type. And you
lose JAXB's advantage of having typed
fields in your Java classes so that
you lose all the checks the Java
compiler might do, and for
unmarshalling you'll have to handle
all the conversions yourself.

Categories