Remove xsi namespace in element - java

I'm using CXF to generate java classes from a WSDL/XSD and later back to XML (for JMS).
In one of the generated classes, it says:
#XmlElement(namespace = "http://www.example.com/", nillable = true)
protected Datum datum;
All good, but when I put a null value:
test.setDatum(null);
I get the following XML generated:
<datum xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
The xsi:nil="true" is what i want, but I don't want the namespace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", because this namespace is already specified at the root element. Also the validation fails at the test platform we have to use.
How do I get rid of the extra namespace in the element?

I did it the other way around. I wrote a interceptor that checks for certain attributes. If found, I add the xsi:nil="true" attribute to the xml.

Related

Getting the xmlns value with Jackson XML?

I'm currently trying to get xmlns value. I need to validate xml files. I'm using Jackson-dataformat-xml to deserialize xml files to objects. So far I've not found a way to get that value.
#JacksonXmlProperty(isAttribute = true) does not seem to be working for xmlns.
xml
<Document xmlns="urn:...">
...
</Document>
Java
#Data
public class Document {
#JacksonXmlProperty(isAttribute = true)
private String xmlns;
}
As I understand xmlns isn't exposed as a attribute. Is there any I could get the value somehow with Jackson?
xmlns value is namespace declarations. it's not a attributes but metadata, so parsers do not expose them as attributes.

How to validate the name property of an element in XML file using javax xml validation

I have the following XML file:
<node name="FIXED_NAME">
...
</node>
Note that all elements in my XML are node (same name) and I can not change them for some reasons.
I am using java xml validator and xsd file for validation.
I want to validate that the name property for the root node must be FIXED_NAME.
How my xsd file should be?
I cannot think of any solution to your problem, for these reasons:
XSD 1.0 identifies the type of a tag by matching the hierarchy of tags in the XML document against the element/type definitions in the XSD. In your case, there is no hierarchy of names (because all tag names are the same).
XSD 1.1 provides some extra flexibility via xs:alternative and xs:assert - but they do not allow references to the parent axis.
For these reasons, I cannot think of any way to use the features of XML Schema to describe your XML.

#XMLRootElement element or array name

I am currently trying to unmarshall a xml get response into one intricate java object. That java object has other classes as fields, so it goes very deep. Now, I am using the JaxbMarshaller2 for this job. So far everything has been going well, but now I am have a question:
Basically one of the tags in the XML stream is called "images" and contains an array of individual image items, each enclosed in the "item" tag.
<images>
<item>
<name></name>
</item>
<item>
<name></name>
</item>
</images>
Now, in my root java class, I have a property called images, which is a list of these image items (I made a custom class for these items.)
#XmlElement(name = "images")
private List<ProductImage> images;
Ok, now inside the ProductImage class, would the XML root element above the class name be #XmlRootElement(name="images") or #XmlRootElement(name="item")?
Thanks for your responses.
The #XmlRootElement annotation is only necessary for the root element of your xml file. Each parent always defines the names of the tags of its children. But since the root element has no parent, there is an additional annotation necessary to define its name (i.e. #XmlRootElement).
Option1:
Assuming that the <images> node is your root node of your xml file.
Create a class ProductImageCollection which has the #XmlRootElement(name="images") annotation.
Inside this class there should be a List<ProductImage> which has an annotation #XmlElement(name="image")
Option2:
Assuming that your <images> node is part of a bigger xml structure, which already has correct annotations.
Then you don't need any additional #XmlRootElement annotations. You can directly annotate your list with #XmlElementWrapper(name="images").

XML element with attribute has ":" in JAXB

I have a problem when I am getting the attributes of a xml file. These attributes are written like this:
#XmlAttribute(name="xml:lang")
With #XmlAttribute I get attributes that they are written on a one word. If I leave this annotation #XmlAttribute(name="xml:lang") I get null
How can I get attribute's value of "xml:lang"?
Thanks.
xml is clearly a namespace prefix. You can try stating the namespace it with:
#XmlAttribute(name = "lang",
namespace = javax.xml.XMLConstants.XML_NS_URI)

Jaxb: attribute fixed value in annotated class

I generate XML schema (XSD) from Java classes with JAXB. I wonder how to specify the value of a static attribute by using annotations.
For example I define an attribute like this
#XmlAttribute(name="tooltip")
private static final String TOOLTIP = "A string";
And I want to get in my XSD
<attribute name="tooltip" type="string" fixed="A string">
So, how can I force the generation of static attributes in XSD with JAXB?
Thanks !
As of JAXB 2.2, there isn't standard JAXB (JSR-222) metadata that can add to your model to cause the fixed attribute to appear in the generated XML Schema. The schema generation errs on the side of being too permissive rather than too restrictive. This means you can't do the following:
Mark a fixed value for an attribute
Mark a maxOccurs other than 1 or unbounded
etc.

Categories