Question on XML parsing - java

When I am parsing an xml string as below I get strange attributes like "autowire" with value "default". Is there anyway I can get only the attributes that are explicitly defined?
<bean id="aaaa" class="com.test.Service">
<property name="cccc" ref="cccc"/>
</bean>
I am doing simple parsing turning it into a Document and then iterating over the Nodes.
Document document = docBuilder.parse(input);
NodeList nodeList = document.getChildNodes();
etc.

You can use following APIs to find whether an attribute is explicitly specified or not:
if you are using DOM:
Attr.getSpecified()
if you are using SAX:
Attributes2.isSpecified(qname)

It depends what you are using to parse. I'm guessing this is a Spring bean configuration file. Usually there's a XML Schema associated with it and that will dictate all default values for attributes.
So when the actual XML parser walks through the document, it will build some kind of representation (DOM parsers will obviously build a tree, SAX parsers will fire off events, etc) of the XML and insert those default values.

Related

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.

Validate JSON data against Yaml shema

I have a java object (let's name it JsonValidator) that can be configured in YAML file.
I would like to describe a schema of JSON objects in YAML notation something like this
And then I need to validate JSON objects according to the schema. Does anybody know any Java libs I can use or any examples?
Thanks
The schema of a json document can be defined using json schema (actually, OpenAPI uses a custom flavor of json schema). It is essentially a json document defining the structure of an other json document. There are a few java implementations out there. If you want to stick to YAML for defining the schema, then will first need to convert YAML to JSON and then use the json schema validator, see this SO question for doing that.
You can find some Java validators for JSON Schema here.

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.

Remove xsi namespace in element

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.

using keyref in xml schema for an attribute with a list of values

I am trying to design a schema for validating an xml format that is already used in an application (not much room for redesigning the xml).
I am trying to utilize the key and keyref elements of the xml schema dictionary to validate identity constraints.
One particular problem is with the way the xml models one to many relationship
<spaceships>
<spaceship guns="gun1 gun2 gun3"/>
</spaceships>
<guns>
<gun id="gun1"/>
<gun id="gun2"/>
<gun id="gun3"/>
</guns>
I came up with this pair of key/keyref in my schema
<xs:key name="gunKey">
<xs:selector xpath="guns/gun" />
<xs:field xpath="#id" />
</xs:key>
<xs:keyref name="gunRef" refer="gunKey">
<xs:selector xpath="spaceships/spaceship" />
<xs:field xpath="#guns" />
</xs:keyref>
This doesn't validate with xerces protesting:
Key 'gunRef' with value 'gun1 gun2 gun3' not found for identity constraint of element.
Is there anyway of expressing in schema that the value of the list is comma separated list of references to another entity and still get the benefit of identity constraint validation?
I'm afraid, that you can not create such reference for an attribute like guns="gun1 gun2 gun3" because gun1 gun2 gun3 is a simple string which is not automatically divided into 3 separate parts.
EDIT 1: If you want to match such attributes, look at this QA: XML schema; multiple from a list of valid attribute values

Categories