I have the following xml:
<parameters>
<parameter >value1</param>
<parameter >value2</param>
</parameters>
with the following class:
#XmlRootElement
public class Parameters {
#XmlElement public String parameter;
}
How can I get warned (exception?) of the duplicate values when unmarshaling the xml to object?
You should have a schema a make a schema validation. If your schema permits duplicates then maybe you should switch from String to a Collection type.
If you don't have a schema, you could use schemagen tool to generate it from your java classes.
You could even generate a schema at runtime, maybe cache it together with the JaxbContext and then use it for validation.
Related
I want to annotate a field in my jaxb generated class with this annotation - #XmlElement(required = false). Which attribute in the XSD would generate my field with this annotation?.
I can't hand type this as the JAXB classes are auto generated using Maven every time a build is run.
My jaxb version is xjc 2.2.4-2
Thanks
When an element has minOccurs="0" the corresponding #XmlElement has required=false. Note that false is the default value of the required attribute so it may not actually appear in the generated annotation.
UPDATE
Based on your comment:
Let me explain my actual problem. I'm using Jackson to generate the
JSON from the JAXB classes. Issue is when the element is not
present in the xml, I see the json output with the field name as 'pip'
and value as null. I am actually expecting the field 'pip' to be
absent from my json output as I declared it to be minOccurs=0 in the
XSD. Can't figure out if it's an issue with JAXB or Jackson.
Interestingly when I annotate the field explicitly with required=false
in the jaxb class, I see my expected output with the field being
absent
This is an issue with Jackson not handling the default value of the required property on the #XmlElement annotation correctly.
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.
I figure this will be easy for someone who really understands JAXB binding files...
Basic Question
How do you configure JAXB to unmarshal multiple elements into the same class?
Note: I want to avoid adding another dependency to my project (like MOXy). Ideally, this can be accomplished with annotations or a custom bindings file.
Background
I have an XML document that contains lots of variations of the same element--each with the exact same properties. Using my example below, all I care about is "Employees" but the XML specifies "directors, managers and staff." For our purposes, these are all subclasses of the same parent and we only need to work with the parent type (Employee) and our object model doesn't have or need instances of the subclasses.
I want JAXB to bind any instance of director, manager, or staff elements into an Employee object.
Example
input:
<organization>
<director>
<fname>Dan</fname>
<lname>Schman</lname>
</director>
<manager>
<fname>Joe</fname>
<lname>Schmo</lname>
</manager>
<staff>
<fname>Ron</fname>
<lname>Schwan</lname>
</staff>
<staff>
<fname>Jim</fname>
<lname>Schwim</lname>
</staff>
<staff>
<fname>Jon</fname>
<lname>Schwon</lname>
</staff>
</organization>
output:
After unmarshalling this example, I would end up with an Organization object with one property: List<Employees> employees where each employee only has a firstName and lastName.
(Note: each employee would be of type Employee NOT Director/Manager/Staff. Subclass information would be lost when unmarshalling. We also don't care about marshaling back out--we only need to create objects from XML)
Can this be done without extensions like MOXy? Can a custom bindings.xjb file save the day?
This corresponds to a choice structure. You could use an #XmlElements annotation for this use case:
#XmlElements({
#XmlElement(name="director", type=Employee.class),
#XmlElement(name="manager", type=Employee.class)
})
List<Employee> getEmployees() {
return employees;
}
If you are starting from an XML schema the following will help:
http://blog.bdoughan.com/2011/04/xml-schema-to-java-xsd-choice.html
I'm looking for best tool/way to create and load JAVA objects from XML definitions.
I had checked out JAXB, seems pretty nice, but didn't find is there a way to work with Entities which properties are dynamic, or changed from time to time, so want to have something like automatic way of working with entities, without converting Object into predefine Entity object. Does something like that exists?
Workflow would be like this read from XML create class for each Entity with dynamic set of attributes and/or create ORM mapping part for those entities and then all manipulation retrieve/store into db or probably will going to use some NoSQL solution like MongoDB.
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.
Check out the following EclipseLink example. It demonstrates how to use dynamic properties with both the JPA and JAXB implementations:
http://wiki.eclipse.org/EclipseLink/Examples/MySports
Option #1 - Static Objects with Dynamic Properties
MOXy has an #XmlVirtualAccessMethods extension which allows you to map entries in a map to XML. This allows you to add properties to a static class. In the example below the Customer class has a "real" name property and may have many "virtual" properties.
package blog.metadatasource.refresh;
import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlVirtualAccessMethods;
#XmlRootElement
#XmlType(propOrder={"firstName", "lastName", "address"})
#XmlVirtualAccessMethods
public class Customer {
private String name;
private Map<String, Object> extensions = new HashMap<String, Object>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object get(String key) {
return extensions.get(key);
}
public void set(String key, Object value) {
extensions.put(key, value);
}
}
The virtual properties are defined via MOXy's XML metadata. In the example below we will add two properties: middleName and shippingAddress.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="blog.metadatasource.refresh">
<java-types>
<java-type name="Customer">
<java-attributes>
<xml-element
java-attribute="middleName"
name="middle-name"
type="java.lang.String"/>
<xml-element
java-attribute="shippingAddress"
name="shipping-address"
type="blog.metadatasource.multiple.Address"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
For More Information
http://blog.bdoughan.com/2011/06/extensible-models-with-eclipselink-jaxb.html
http://blog.bdoughan.com/2011/06/moxy-extensible-models-multi-tenant.html
http://blog.bdoughan.com/2011/06/moxy-extensible-models-multiple.html
http://blog.bdoughan.com/2011/06/moxy-extensible-models-refresh-example.html
Option #2 - Dynamic Objects
MOXy also offers full dynamic object models:
DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(inputStream);
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set(street, "123 A Street");
address.set(city, "Any Town");
customer.set("address", address);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(customer, System.out);
For More Information
http://wiki.eclipse.org/EclipseLink/UserGuide/MOXy
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic
So, you're basically trying to make POJO's (plain old Java objects) using XML files? They are just like data classes, right?
I'm a big fan of XStream, which is really easy to use and works great if you don't need validation. I've used Castor when validation against a schema was required. I just used XStream to save an object to an xml file and then I can read it back in from anywhere, even if I change the data values associated with the object (which I think is what you mean by "dynamic set of attributes", right?).
How can I covert a an xml file to a simple java bean?
Its a simple xml file without any xsd, which was generated from a java bean, which I don't have access to.
I tried using xmlbeans to first generate the xmd from xml and then to generate classes from the xsd. I got a bunch of classes. I am looking for a single java bean class.
JAXB
JAXB (JSR-222) provides an easy way to convert objects to XML. There are many open source implementations of this standard including:
Metro JAXB (the reference implementation included in Java SE 6)
EclipseLink JAXB (MOXy), I'm the tech lead
Apache JaxMe
JAXB has a default mapping for Java objects to XML. This mapping can be customized through the application of annotations.
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.Element;
#XmlRootElement
public class Address {
private String street;
private String city;
private String state;
private String country;
#XmlElement(name="postal-code")
private String postalCode;
}
Would correspond to the following XML:
<address>
<street>123 A Street</street>
<city>Any Town</city>
<state>A State</state>
<postal-code>12345</postal-code>
</address>
EclipseLink JAXB (MOXy)
MOXy has an XPath based mapping extension. This means we can take our same Address class and map it to Google's geocode format:
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="kml")
#XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
public class Address {
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
private String street;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
private String city;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
private String state;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
private String country;
#XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
private String postalCode;
}
The above class corresponds to the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0" xmlns:ns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<Response>
<Placemark>
<ns:AddressDetails>
<ns:Country>
<ns:CountryNameCode>US</ns:CountryNameCode>
<ns:AdministrativeArea>
<ns:AdministrativeAreaName>CA</ns:AdministrativeAreaName>
<ns:SubAdministrativeArea>
<ns:Locality>
<ns:LocalityName>Mountain View</ns:LocalityName>
<ns:Thoroughfare>
<ns:ThoroughfareName>1600 Amphitheatre Pkwy</ns:ThoroughfareName>
</ns:Thoroughfare>
<ns:PostalCode>
<ns:PostalCodeNumber>94043</ns:PostalCodeNumber>
</ns:PostalCode>
</ns:Locality>
</ns:SubAdministrativeArea>
</ns:AdministrativeArea>
</ns:Country>
</ns:AddressDetails>
</Placemark>
</Response>
</kml>
For more Information
XPath Based Mapping - Geocode Example
Map to Element based on an Attribute Value with EclipseLink JAXB (MOXy)
XPath Based Mapping
Try Castor Mapping.
You could use a tool like Castor or JAXB to map the XML to a java class. Castor is fairly easy to use.