I tried the approach in this post
However I am getting a
>
1 counts of IllegalAnnotationExceptions
XmlIDREF property is referencing a type "java.lang.String" that doesn't have an XmlID property.
this problem is related to the following location:
at private externalReferences.Department
externalReferences.Employee.department
at externalReferences.Employee
at private java.util.List externalReferences.Company.employees
at externalReferences.Company
The two xml Files are the following:
employee.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
<employeeList>
<employee name="Jane Doe" id="1">
<department>1</department>
</employee>
<employee name="John Smith" id="2">
<department>2</department>
</employee>
<employee name="Anne Jones" id="3">
<department>3</department>
</employee>
</employeeList>
</company>
department.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<departmentList>
<departmentList>
<department name="Dev" id="1"/>
<department name="Sales" id="2"/>
<department name="Research" id="3"/>
</departmentList>
</departmentList>
The employee.xml references the department and I want to point to the right department objects when unmarshalling employee.xml.
Classes are as follows:
Company.java
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Company {
#XmlElementWrapper(name = "employeeList")
#XmlElement(name="employee")
private List<Employee> employees;
#XmlElementWrapper(name = "departmentList")
#XmlElement(name="department")
private List<Department> departments;
public Company() {
employees = new ArrayList<Employee>();
departments = new ArrayList<Department>();
}
...
}
Employee.java
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
#XmlAttribute
#XmlID
private String id;
public String getId() {
return id;
}
#XmlIDREF
private Employee manager;
#XmlJavaTypeAdapter(EmpAdapter.class)
#XmlIDREF
private Department department;
}
Department.java
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Department {
#XmlAttribute
#XmlID
private String id;
...
}
DepartmentList.java
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class DepartmentList {
#XmlElementWrapper(name = "departmentList")
#XmlElement(name="department")
private List<Department> departments;
Then I run the following in Main
JAXBContext jc = JAXBContext.newInstance(DepartmentList.class); Unmarshaller unmarshaller = jc.createUnmarshaller();
DepartmentList depList = (DepartmentList) unmarshaller.unmarshal(new FileReader(DepRef));
EmpAdapter adapter = new EmpAdapter();
for(Department dep : depList.getDepartments()) {
adapter.getDepList().put(dep.getId(), dep);
}
JAXBContext jc2 = JAXBContext.newInstance(Company.class);
Unmarshaller unmarshaller2 = jc2.createUnmarshaller();
unmarshaller2.setAdapter(adapter);
Company company2 = (Company) unmarshaller2.unmarshal(new FileReader(empRef));
I feel that having one XMLIDREF refer to employee id and the other XMLIDREF refer to department id is part of the problem. But that is required since the manager field references other employee objects.
Can someone please help me with this. Thank you
The problem arises from class Company that corresponds to an XML document containing employees and departments. However, you've got two separate documents. Apparently you want one final class containing both lists.
(1) You could define a class EmployeeList for employees only, similar to the one for departments (DepartmentList). This will still let you write an application class Company into which you set the references for both lists.
(2) Change the annotation for Company.departments
#XmlTransient
private List<Department> departments;
marshal like you do now, and set the List with the reference you have from unmarshalling the corresponding XML into the returned object.
Related
I have the following entities defined after my database model:
#Entity
#Table(name = "PERSON")
#XmlRootElement(name = "person")
public class Person implements java.io.Serializable {
#Id
private Long personId;
private String personName;
#XmlElementWrapper(name="children")
#XmlElement(name="child")
private Set<Child> children = new HashSet<Child>(0);
// getters & setters
}
#Entity
#Table(name = "CHILD")
#XmlRootElement(name = "child")
public class Child implements java.io.Serializable {
#EmbeddedId
private ChildId id;
private Date childName;
// getters & setters
}
#Embeddable
public class ChildId implements java.io.Serializable {
#XmlTransient
private Person parentId;
private Date childBirthDate;
private int childOrder;
// getters & setters
}
In the current state of thing, when marshalling using JAXB here is what I get:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<personId>7</personId>
<personName>Foo</personName>
<children>
<child>
<childName>Bar</childName>
<id>
<childBirthDate>2001-06-22</childBirthDate>
<childOrder>1</childOrder>
</id>
</child>
</children>
</person>
What I would like to get instead is something like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<personId>7</personId>
<personName>Foo</personName>
<children>
<child>
<childName>Bar</childName>
<childBirthDate>2001-06-22</childBirthDate>
<childOrder>1</childOrder>
</child>
</children>
</person>
Is there a way for me to get the ChildId's fields to the same level as the other Child's fields?
Please advise what my options are.
Moving comment to answer:
As multiple #Ids are possible in the same class, you could simply move the fields from the ChildId class into the Child class. Then you annotate each field with #Id (the fields which were moved from ChildId). This way you have all fields on the same level as they are in the same class, while also retaining the functionality of #EmbeddedId
I want to unmarshal my below xml file using JAXB.
<School>
<Student>
<Name> My xyz<Name>
<Hobbies> Playing Cricket <sup>+</sup> Watching TV</Hobbies>
</Student>
</School>
In Above example , There is one school with student which having hobbies.
Whenever i unmarshal above xml using (#XmlPath) i got only "Watching TV" but not "Playing Cricket".
I have tried to unmarshal using "DomHandler" also but not success.
Thanks.
I think your XML should be something like this:
<?xml version="1.0" encoding="UTF-8"?>
<School>
<Student>
<Name>My xyz</Name>
<Hobbies>
<Name>Playing Cricket</Name>
<Hobby>
</Hobby>
<Name>Playing Cricket</Name>
</Hobbies>
</Student>
<!-- OTHER STUDENTS FOLLOW-->
</School>
and then the correspondant java object structure would be something like:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "School")
class School {
#XmlElement(required = true, name = "Student")
Student student;
}
#XmlAccessorType(XmlAccessType.FIELD)
class Hobby {
#XmlElement(required = true, name = "Name")
String name;
}
#XmlAccessorType(XmlAccessType.FIELD)
class Student {
#XmlElement(required = true, name = "Name")
String name;
#XmlElement(required = true, name = "Hobbies")
List<Hobby> hobbies;
}
I took off the getters and the setters for brevity.
I am looking to control where JAXB generates the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" declaration when marshalling to XML. I've seen solutions like this to add it to the root element using the JAXB_SCHEMA_LOCATION property, however I don't want it on the root node, I want it somewhere in between. Here's what I've got:
#XmlRootElement(name = "RootNode")
#XmlAccessorType(XmlAccessType.NONE)
public class RootNode {
#XmlElement(name = "IntermediateNode")
private IntermediateNode intermediateNode;
//getter & setter
}
#XmlAccessorType(XmlAccessType.NONE)
public class IntermediateNode {
#XmlElement(name = "MyEntity")
private MyEntity myEntity;
//getter and setter
}
#XmlAccessorType(XmlAccessType.NONE)
public class MyEntity {
#XmlElement(name = "Name")
private String name;
#XmlElement(name = "Title", nillable = true)
private String title;
//getters and setters
}
Serialize like:
MyEntity myEntity = new MyEntity();
myEntity.setName("George");
myEntity.setTitle(null);
IntermediateNode intNode = new IntermediateNode();
intNode.setMyEntity(myEntity);
RootNode rootNode = new RootNode();
rootNode.setIntermediateNode(intNode);
JAXBContext context = JAXBContext.newInstance(RootNode.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(rootNode, System.out);
Produces XML like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootNode>
<IntermediateNode>
<MyEntity>
<Name>George</Name>
<Title xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</MyEntity>
</IntermediateNode>
</RootNode>
But what I want is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootNode>
<IntermediateNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyEntity>
<Name>George</Name>
<Title xsi:nil="true"/>
</MyEntity>
</IntermediateNode>
</RootNode>
I even tried moving my IntermediateNode and MyEntity classes into their own package with a package-info.java like this, but that just rolled the xmlns:xsi up to the root element.
#javax.xml.bind.annotation.XmlSchema(
xmlns = {
#javax.xml.bind.annotation.XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.example.intermediate;
Is it possible to get what I want?
Is it possible to unmarshal a XML file like this:
<company id="bh23" name="imob">
<store>
<store-info id="2392">
<address>NYC</address>
<name>Imob's NYC 5th</name>
</store>
<products>
<product>
<name>keyboard</keyboard>
<price>2000</price>
</product>
<product>
<name>mouse</keyboard>
<price>1000</price>
</product>
</products>
</store>
<store />
</stores>
into classes like these:
#XmlElementRoot(name = "company")
public class Company {
#XmlAttribute (name = "id")
String id;
#XmlAttribute (name = "name")
String name;
#XmlElement (name = "store")
List<Store>stores;
//all the getters and setters
}
#XmlElementRoot (name = "store")
public class Store {
#XmlAttribute (name = "id")
String id;
#XmlElement (name = "address")
String address;
#XmlElement (name = "name")
String name;
#XmlElementWrapper (name = "products")
#XmlElement (name = "product")
List<Product>products;
//all the getters and setters
}
public class main {
public static void main (String args[]) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Company portfolio = (Company) jaxbUnmarshaller.unmarshal(new File(xmlUrlPath));
System.out.println(portfolio.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
I'm trying to "skip" or "jump" the node named "store-info" because I don't want to create another class just to keep the store's address and name, since it would be more simple to "append" both address and name to "Store" class.
Of course, when I run the code, the vars "address", "id" and "name" becomes null and only the list of products is correctly unmarshaled.
Is there a way to skip a node, merging their fields into another class? I'm avoiding (for "legal" purposes) the use of MOXy lib and their XPath annotation.
You could create a StAX filtered XMLStreamReader and have JAXB unmarshal that to ignore one or more XML elements.
http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html#createFilteredReader%28javax.xml.stream.XMLStreamReader,%20javax.xml.stream.StreamFilter%29
Below is a link to a full example I gave in an answer to a similar question:
JAXB filtered parsing
I am trying to unmarshall an XML document from a legacy system using JAXB. I have an xml structure as follows :
<response>
<id>000000</id>
<results>
<result>
<!-- Request specific xml content -->
<year>2003</year>
<title>Lorem Ipsum</title>
<items>
<item>I1</item>
<item>I2</item>
</items>
</result>
<result>
<year>2007</year>
<title>Dolor sit amet</title>
<items>
<item>K1</item>
<item>K2</item>
</items>
</result>
</results>
</response>
The tags inside the part specified by <result> tag will change depending on my request. Since the content may change I decided to use generics for result items and I have prepared my java beans with annotations as following:
// imports here
#XmlRootElement(name="response")
#XmlAccessorType(XmlAccessType.FIELD)
public class XResponse<T>{
private String id;
#XmlElementWrapper(name="results")
#XmlElement(name="result")
private List<T> results;
// setters and getters
}
...
#XmlRootElement(name="result")
#XmlAccessorType(XmlAccessType.FIELD)
public class X1Result{
private String year;
private String title;
#XmlElementWrapper(name="items")
#XmlElement(name="item")
private List<String> items;
// setters and getters
}
...
I tried unmarshalling the xml document via the code below:
JAXBContext context = JAXBContext.newInstance(XResponse.class, X1Result.class);
Unmarshaller um = context.createUnmarshaller();
XResponse<X1Result> response = (XResponse<X1Result>) um.unmarshal( xmlContent );
List<X1Result> results = unmarshal.getResults();
for (X1Result object : results) {
System.out.println(object.getClass());
}
I have a problem during the unmarshalling that it can't cast the list items into X1Result class. Instead it uses org.apache.xerces.dom.ElementNSImpl.
What should I do to make JAXB Unmarshaller use X1Result class?
Thanks in advance
I think you should use inheritance instead of generics. Given an XML like this:
<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>000000</id>
<results>
<result xsi:type="X1Result">
<year>2003</year>
<title>Lorem Ipsum</title>
<items>
<item>I1</item>
<item>I2</item>
</items>
</result>
<result xsi:type="X1Result">
<year>2007</year>
<title>Dolor sit amet</title>
<items>
<item>K1</item>
<item>K2</item>
</items>
</result>
</results>
</response>
You can dynamically bind your <result> entries. You have a top-level type:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "XResult")
#XmlSeeAlso({
X1Result.class
})public abstract class XResult {
}
And you have implementing classes:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "X1Result")
public class X1Result extends XResult {
#XmlElement(name = "year")
private String year;
#XmlElement(name = "title")
private String title;
#XmlElementWrapper(name = "items")
#XmlElement(name = "item")
private List<String> items;
...
}
Use the top-level type in your XResponse class:
#XmlRootElement(name = "response")
#XmlAccessorType(XmlAccessType.FIELD)
public class XResponse {
#XmlElement(name = "id")
private String id;
#XmlElementWrapper(name = "results")
#XmlElement(name = "result")
private List<XResult> results;
...
}
And you can unmarshall using the top-level type:
context = JAXBContext.newInstance(XResponse.class, XResult.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
XResponse response = (XResponse) unmarshaller.unmarshal(new File("testfile.xml"));
List<XResult> results = response.getResults();
for (XResult object : results) {
System.out.println(object.getClass());
}