Since I feel that it's too much to assume Castor would be able to
automatically figure out that the Person you are trying to map is really
an inner-class...
Here's how I'd do it:
Person p = new Person();
Person1V p1V = p.getPerson();
Unmarshaller unm = new Unmarshaller(p1V);
unm.setReuseObjects(true);
unm.unmarshal(new FileReader("test2.xml"));
--Keith
Sudeep Sahdeva wrote:
>
> Hi,
> I am trying to use Castor to marshall/unmarshall xml to java objects.
> My java class looks like this:
>
> // Person.java
> //
> //
>
> // Imports
> //
> import java.math.BigDecimal;
> import java.io.IOException;
>
> /**
> * DataView class for Person buffers.
> */
> public final class Person
> /**
> * Construct an empty view.
> */
> public Person()
> {
> m_person = new Person1V();
> }
>
>
> /**
> * Construct a new (empty) instance of this class.
> */
> public DataView newInstance()
> {
> return new Person();
> }
>
>
>
> /**
> * DataView subclass for person Group.
> */
> public final class Person1V extends DataView
> {
> /**
> * Construct an empty view.
> */
> public Person1V()
> {
> m_personName = "";
> m_personAddress = "";
> m_personSsn = "";
> m_personEmail = "";
> m_personHomePhone = "";
> m_personWorkPhone = "";
> }
>
> /**
> * Construct a new (empty) instance of this class.
> */
> public DataView newInstance()
> {
> return new Person1V();
> }
>
>
> // ---------------------------------------------------------------
> // Code for field "person-name"
> //
> private String m_personName;
>
> /**
> * Set the value of the personName field.
> */
> public void setPersonName(String value)
> {
> m_personName = value;
> }
>
> /**
> * Get the current value of the personName field.
> */
> public String getPersonName()
> {
> return m_personName;
> }
>
> // ---------------------------------------------------------------
> // Code for field "person-address"
> //
> private String m_personAddress;
>
> /**
> * Set the value of the personAddress field.
> */
> public void setPersonAddress(String value)
> {
> m_personAddress = value;
> }
>
> /**
> * Get the current value of the personAddress field.
> */
> public String getPersonAddress()
> {
> return m_personAddress;
> }
>
> // ---------------------------------------------------------------
> // Code for field "person-ssn"
> //
> private String m_personSsn;
>
> /**
> * Set the value of the personSsn field.
> */
> public void setPersonSsn(String value)
> {
> m_personSsn = value;
> }
>
> /**
> * Get the current value of the personSsn field.
> */
> public String getPersonSsn()
> {
> return m_personSsn;
> }
>
> // ---------------------------------------------------------------
> // Code for field "person-email"
> //
> private String m_personEmail;
>
> /**
> * Set the value of the personEmail field.
> */
> public void setPersonEmail(String value)
> {
> m_personEmail = value;
> }
>
> /**
> * Get the current value of the personEmail field.
> */
> public String getPersonEmail()
> {
> return m_personEmail;
> }
>
> // ---------------------------------------------------------------
> // Code for field "person-home-phone"
> //
> private String m_personHomePhone;
>
> /**
> * Set the value of the personHomePhone field.
> */
> public void setPersonHomePhone(String value)
> {
> m_personHomePhone = value;
> }
>
> /**
> * Get the current value of the personHomePhone field.
> */
> public String getPersonHomePhone()
> {
> return m_personHomePhone;
> }
>
> // ---------------------------------------------------------------
> // Code for field "person-work-phone"
> //
> private String m_personWorkPhone;
>
> /**
> * Set the value of the personWorkPhone field.
> */
> public void setPersonWorkPhone(String value)
> {
> m_personWorkPhone = value;
> }
>
> /**
> * Get the current value of the personWorkPhone field.
> */
> public String getPersonWorkPhone()
> {
> return m_personWorkPhone;
> }
> }
>
> // ---------------------------------------------------------------
> // Code for field "person"
> //
> private Person1V m_person;
>
> /**
> * Get the current value of the person field.
> */
> public Person1V getPerson()
> {
> return m_person;
> }
> }
>
> // END Person.java
>
> Now my xml (test2.xml) looks like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <person>
> <personName>Michael Owen</personName>
> <personAddress>222 Bazza Lane, Liverpool, MN</personAddress>
> <personSsn>111-222-3333</personSsn>
> <personEmail>[EMAIL PROTECTED]</personEmail>
> <personHomePhone>720.111.2222</personHomePhone>
> <personWorkPhone>111.222.3333</personWorkPhone>
> </person>
>
> Using the following code with Castor does not work:
>
> import org.exolab.castor.xml.*;
> import java.io.FileReader;
> import org.apache.xml.serialize.XMLSerializer;
>
> try {
>
> Person person = (Person)
> Unmarshaller.unmarshal(Person.class,
> new FileReader("C:/Projects/jam/dat/test2.xml"));
> System.out.println("Test1 Attributes");
> System.out.println("-----------------");
> System.out.println("Name: " + person.getPerson().getPersonName() );
> System.out.println("Address: " + person.getPerson().getPersonAddress()
> );
> System.out.println("SSN: " + person.getPerson().getPersonSsn() );
> System.out.println("Email: " + person.getPerson().getPersonEmail() );
> System.out.println("Home Phone: " +
> person.getPerson().getPersonHomePhone() );
> System.out.println("Work Phone: " +
> person.getPerson().getPersonWorkPhone() );
> } catch (Exception e) {
> System.out.println( e );
> }
>
> This does not work, would appreciate if some can help. In advance, Thanks
> for your help ......
>
> Sudeep
>
> <<test2.xml>>
>
> ------------------------------------------------------------------------
>
> test2.xmlName: test2.xml
> Type: XML Document (text/xml)
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev