File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes jaxb-XSD issue, pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "jaxb-XSD issue, pls help" Watch "jaxb-XSD issue, pls help" New topic
Author

jaxb-XSD issue, pls help

Sudheera Liyanage
Greenhorn

Joined: Oct 14, 2002
Posts: 7
I have xsd which contains a PERSON abstract element.
Implementations of the PERSON are EMPLOYEE and CUSTOMER.

when i marshal the object, it returns only attributes in the PERSON class.
EMPLOYEE and CUSTOMER attributes are not getting.


XSD----------------
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<xs:element name="Users">
<xs:complexType>
<xs:sequence>
<xs:element name="person" type="Person"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>



<xs:complexType name="Person" abstract="true">
<xs:sequence>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:sequence>
</xs:complexType>



<xs:complexType name="Employee">
<xs:complexContent>
<xs:extension base="Person">
<xs:sequence>
<xs:element name="employeeId" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="Customer">
<xs:complexContent>
<xs:extension base="Person">
<xs:sequence>
<xs:element name="customerId" type="xs:string" />
<xs:element name="companyName" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>



Test class --------------------
package jaxb.reservation.util;

import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.XMLGregorianCalendar;

import jaxb.person.dto.Customer;
import jaxb.person.dto.Employee;
import jaxb.person.dto.Person;
import jaxb.person.dto.Users;
import jaxb.reservation.dto.Adults;
import jaxb.reservation.dto.ContactDetail;
import jaxb.reservation.dto.DefaultReservationRequest;
import jaxb.reservation.dto.DefaultSpecialServiceRequest;
import jaxb.reservation.dto.EmergencyContactDetailSSR;
import jaxb.reservation.dto.PassportSSR;
import jaxb.reservation.dto.SSRs;
import jaxb.reservation.dto.SpecialServiceRequest;

import jaxb.reservation.dto.Infants;
import jaxb.reservation.dto.MessageIdentifier;
import jaxb.reservation.dto.Adult;
import jaxb.reservation.dto.Infant;

public class PersonUtil {

/**
* creates xml, representing the given object
*
* @param object
* @return
*/
public static String createXMLString(Object object) {
try {
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();

marshaller.marshal(object, sw);

return sw.toString();
} catch (Exception e) {
System.out.println(e);
}
return null;

}

public static Object createObject(String xmlString, Class objClass) {
ByteArrayInputStream xmlContentBytes = new ByteArrayInputStream(xmlString.getBytes());
Object xmlObject;

try {
JAXBContext context = JAXBContext.newInstance(objClass);
Unmarshaller unmarshaller = context.createUnmarshaller();

// setting schema to null will turn validator off
unmarshaller.setSchema(null);
xmlObject = objClass.cast(unmarshaller.unmarshal(xmlContentBytes));

return xmlObject;
} catch (JAXBException e) {
System.out.println(e);
}
return null;
}

public static void main(String[] args) throws DatatypeConfigurationException {
marshal();

// unmarshal();
}

// marshalling
private static void marshal() throws DatatypeConfigurationException {
Users users = new Users();
String xml = null;

Employee employee = new Employee();
Customer customer = new Customer();

employee.setFirstName("Sudheera");
employee.setLastName("Liyanage");
employee.setEmployeeId("E10000");

customer.setFirstName("Nilindra");
customer.setLastName("Fernanado");
customer.setCustomerId("C10000");
customer.setCompanyName("JKCS");

users.getPerson().add(employee);
users.getPerson().add(customer);

xml = createXMLString(users);

System.out.println(xml);

}


}


result XML --------------------

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Users><person><firstName>Sudheera</firstName><lastName>Liyanage</lastName></person><person><firstName>Nilindra</firstName><lastName>Fernanado</lastName></person></Users>
__________________
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jaxb-XSD issue, pls help