I use an XML Schema Definition and JAXB to generate Java classes with proper #XmlElement or #XmlRootElement annotations.
Since the schema has some deep nesting in it (not my choice), I'd rather use jxpath to access deeply buried classes using an XPath (rather than cumbersome daisy-chain of .getThat() and that != null).
The problem is that some of the XML element names contain dashes, e.g. foo-bar. When I try to access elements using org.apache.jxpath, I need to rewrite my XPath so that such names are camel-cased instead (fooBar) like the name of the actual Java objects. Is there any way to tell jxpath to find the elements using the XPath corresponding to the XML element names (instead of the camel-cased Bean names)?
I think it is related to this question, however in my case I don't actually case what kind of tricks and decorations are used on the auto-generated classes, as long as xjc can do it.
Here is a simple example to illustrate the issue.
First, a small XSD file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="record">
<xs:complexType>
<xs:sequence>
<xs:element name="foo-bar" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Compile it...
xjc -p org.pd.jx example.xsd
Here is the xjc-generated Record.java class (minus comments):
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {"fooBar"})
#XmlRootElement(name = "record")
public class Record {
#XmlElement(name = "foo-bar", required = true)
protected String fooBar;
public String getFooBar() {return fooBar;}
public void setFooBar(String value) {this.fooBar = value;}
}
Then, trying to access the data via jxpath (in practice I have to deal with lots of deeply nested classes), one can see below that the correct XPath ("foo-bar") doesn't work, but a camel-cased version does.
Record record = new Record();
record.setFooBar("hello world");
JXPathContext context = JXPathContext.newContext(record);
context.setLenient(true);
String a = (String)context.getValue("foo-bar", String.class); // is null
String b = (String)context.getValue("fooBar", String.class); // "hello world"
I believe JXPath operates on an objects properties and not the XML elements they correspond to. I do not believe that JXPath parses any of the JAXB metadata so it wouldn't know the XML nodea you have mapped. What you are seeing appears to be the expected behaviour.
Related
I have an XML file that looks like this
<coordinate>
<url>http://some-url</url>
<properties>
<property type="integer">key1=12345</property>
<property visibility="private" type="string">key2=value2</property>
</properties>
</coordinate>
I would like that to generate an Object structure that looks like this:
class Coordinate
string url
List<Property> properties
class Property
string type
string visibility
Jaxb seems to require a "Properties" type layer in between the coordinate element and the property elements. Is there any way to just get it to add the properties directly to the list contained in the Coordinate object?
JAXB #XmlElementWrapper annotation can be used for this scenario as below.
#XmlElementWrapper(name = "properties")
#XmlElement(name="property")
protected List<Property> propertyList = new ArrayList<Property>();
for example i have this xml:
<xml>
<a>1</a>
<b>2</b>
</xml>
and this class:
#XmlRootElement(name = "xml")
public class xml{
int aPlusb;
....
}
i know how to create an XmlAdapter but i would like it to set the filed aPlusb to be the plus between the value of element a and the value of filed b.
is there a way to do this in JAXB without making an XmlTransient field and calculate it separately?
I have the schema like this :
<xsd:complexType name="ContentType" mixed="true">
<xsd:annotation>
<xsd:documentation><![CDATA[
The content type is a broad base type allowing any content.
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="BaseContentType">
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded" namespace="##any" processContents="lax"
/>
</xsd:sequence>
<xsd:attribute name="orientation" type="OrientationEnum" use="optional"
default="portrait">
<xsd:annotation>
<xsd:documentation><![CDATA[
The #orientation attribute is used to specify a "landscape"
orientation for the published form. This is primarily used
for schedules or for tables.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
I use xjc command line tool to generate the Java classes from the above schema and the classes are generated as follows:
public abstract class BaseContentType {
#XmlMixed
protected List<Serializable> content;
#XmlAttribute(name = "id")
#XmlJavaTypeAdapter(CollapsedStringAdapter.class)
#XmlID
#XmlSchemaType(name = "ID")
protected String id;
and
public class ContentType
extends BaseContentType
{
#XmlMixed
#XmlAnyElement(lax = true)
#OverrideAnnotationOf
protected List<Object> contentOverrideForContentType;
When I unmarshall, all the nested xml elements get populated in the List contentOverrideForContentType of the ContentType object and all the text elements get populated in the List content of BaseContentType.
How do I determine the order of the text elements with respect to the nested elements and construct the entire text?
I am trying to get the entire text within the ContentType for which I have to look at top level text and the text of all the nested tags and combine them all(here is where I need to know the order). Is there a better way to just extract all the text from ContentType?
Thanks!
EDIT
This is related to this question.
If you declare a propOrder on the specific jaxB class you can specify the order that objects will appear in the xml
e.g.
#XmlType(propOrder = {
"firstObjectName",
"fsecondObjectName"
})
note: when using a propOrder all elements in the object must be added to the propOrder.
I had the same issue and I found out that the elements will be ordered following the order in which the elements had been added to the list ... this is assuming you are using an ordered list (List, ArrayList...)
I am getting NULL value eventhough there is some value in xml using jaxb unmarshalling.
Element in xsd
<xs:element name="Account" minOccurs="1" maxOccurs="unbounded" >
does not have type attribute.
In its respective Java class AccountNo become java.lang.Object type.
but after Unmarshalling xml
<AccountNo>GDF23232223</AccountNo>
output comes [AccountNo=NULL]
My unmarshalling code
jaxbContext = JAXBContext.newInstance("net.process");
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
document = (Document) jaxbUnmarshaller.unmarshal(inputFile);
can you help me?
Thanks
I struggled with the same problem. Try to get the value using
element.getFirstChild().getNodeValue()
There I got the correct value
1.
<xsd:element name="tuple">
<xsd:complexType>
<xsd:sequence>
<xsd:any minOccurs="0" maxOccurs="unbounded" namespace="##any" processContents="skip" />
</xsd:sequence>
</xsd:complexType>
2.
<xsd:element name="tuple"/>
Are the both above schemas equivalent?
Please help
NO! They aren't same!
In first schema: element <tuple> can have 0 or more elements under it with/of any name, that is because you have added <any> tag with minOccurs="0" and maxOccurs="Unbounded" under complexType definition of element tuple
Also Since you have specified processContents="skip" under <any> it doesn't bother anything about its child elements..
Example:
<tuple>
<foo>data</foo>
<bar foobarattribute="foobardata">data</bar>
</tuple>
As you can see, though foo and bar and its attribute foobarattribute are not defined in XSD, it allows this XML as valid. But it doesn't allow attribute under <tuple>
EDIT:
Second schema:
It is as same as declaring the type as anyType
<xsd:element name="tuple" type="xs:anyType"/>
Which means it allows any element and any attribute under tuple. The sample XML would be like:
<tuple foo="bar">
<foobar attrb="value">data</foobar>
</tuple>
You can observe that unlike first sample XML, this has attribute under <tuple> as well.
One more important difference between two sample XSD code is:
In 1st code you are specifying processContents as "skip": Because of which XML processor does not attempt to validate any elements from the specified namespaces..
In the 2nd Code, for anyType, processContents would be "lax": because of which the XML processor tries to obtain the schema for the required namespaces and validate the elements.
Hope it helps..