Java Mailing List Archive

http://www.junlu.com/

Home » user.castor »

[castor-user] Fwd: Problem with unmarshalling if xml file contains information
about xsd

Małgorzata Wolniewicz

2012-08-21

Replies: Find Java Web Hosting

Author LoginPost Reply


------ Wiadomość oryginalna ------
Temat: Problem with unmarshalling if xml file contains information about xsd
Data: Tue, 21 Aug 2012 10:44:54 +0200
Nadawca: Małgorzata Wolniewicz <gosiaw@man.poznan.pl>
Odpowiedź-Do: Małgorzata Wolniewicz <gosiaw@man.poznan.pl>
Firma/Organizacja: Poznań Supercomputing and Networking Center
Adresat: user@castor.codehaus.org


Dear Castor users,

I have encountered problem, which seems to be trivial, anyway I have no 
idea how to solve it.
I created the simplest example I could. I want to have an xml file, 
based on rules defined by my xsd schema, and a java client which will 
perform unmarshalling.
My client works if the xml looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<person pesel="12345"
 >
<firstname>Gosia</firstname>
<lastname>Wolniewicz</lastname>
<address>My address</address>

<children>
<child>
<data name="Kid1" age="8.0"/>
</child>
<child>
<data name="Kid2" age="4.5"/>
</child>
</children>
</person>

But I would like to include in my xml information which xsd defines it, 
but when I add it my client stops working. It does not crash, but data 
is not unmarshalled properly. My object has empty fields.
With schema information begin of my xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<person pesel="12345"
xmlns="http://www.example.org/Person"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/Person Person.xsd "
 >
...

I enclose my xml (with and without the header), my client and my xsd. I 
would be grateful if anyone could suggest how to solve the problem.

Best regards,
Gosia Wolniewicz

-- 
Małgorzata Wolniewicz
Poznań Supercomputing and Networking Center
Scientific Applications Department

email: gosiaw@man.poznan.pl
www: http://www.man.poznan.pl
skype: gosiawolniewicz
tel. +48 61 858-21-74


<?xml version="1.0" encoding="UTF-8"?>
<person pesel="12345"
xmlns="http://www.example.org/Person"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/Person Person.xsd "
>
<firstname>Gosia</firstname>
<lastname>Wolniewicz</lastname>
<address>My address</address>

<children>
 <child>
   <data name="Kid1" age="8.0"/>
 </child>
 <child>
 <data name="Kid2" age="4.5"/>
 </child>
 </children>
</person>
<?xml version="1.0" encoding="UTF-8"?>
<person pesel="12345"
>
<firstname>Gosia</firstname>
<lastname>Wolniewicz</lastname>
<address>My address</address>

<children>
 <child>
   <data name="Kid1" age="8.0"/>
 </child>
 <child>
 <data name="Kid2" age="4.5"/>
 </child>
 </children>
</person>
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;

/**
*
*/

/**
* @author gosiaw
*
*/
public class CastorUnmarshallingExample {

 /**
  *
  */
 public CastorUnmarshallingExample() {
   // TODO Auto-generated constructor stub
 }

 /**
  * @param args
  * @throws ValidationException
  * @throws MarshalException
  * @throws MappingException
  * @throws IOException
  */
 public static void main(String[] args) throws MarshalException, ValidationException, IOException, MappingException {
    Mapping mapping = new Mapping();

    try {
     mapping.loadMapping("/Users/gosiaw/Vine/XMLTests/src/Mappings.xml");
     FileReader reader = new FileReader("/Users/gosiaw/Vine/XMLTests/src/Test.xml");
     Unmarshaller unmarshaller = new Unmarshaller(Person.class);
            unmarshaller.setMapping(mapping);
     unmarshaller.setValidation(true);      
     Person person = (Person)unmarshaller.unmarshal(reader);
     System.out.println("Pesel: " + person.getPesel());
     System.out.println("First Name: " + person.getFirstName());
     System.out.println("Last Name: " + person.getLastName());
     System.out.println("Address: " + person.getAddress());
     List<Child> children = person.getChildren();
     if (children != null) {
      System.out.println("Children:");
      for (Iterator<Child> i = children.iterator(); i.hasNext(); ) {
        Child child = (Child)i.next();
        System.out.println("Name: " + child.getName());
        System.out.println("Age: " + child.getAge());
      }
     } else {
      System.out.println("No children");
     }
   
    } catch (Exception e) {
     System.err.println(e.getMessage());
     e.printStackTrace(System.err);
    }
  }
}
import java.util.List;

/**
*
*/

/**
* @author gosiaw
*
*/
public class Person {
 private java.lang.String pesel;
 private java.lang.String firstName;
 private java.lang.String lastName;
 private java.lang.String address;
 private List<Child> children;

 public Person() {    
 }
 
 public java.lang.String getPesel() {
   return pesel;
 }

 public void setPesel(java.lang.String pesel) {
   this.pesel = pesel;
 }
 
 public java.lang.String getFirstName() {
   return firstName;
 }

 public void setFirstName(java.lang.String firstName) {
   this.firstName = firstName;
 }

 public java.lang.String getLastName() {
   return lastName;
 }

 public void setLastName(java.lang.String lastName) {
   this.lastName = lastName;
 }

 public java.lang.String getAddress() {
   return address;
 }

 public void setAddress(java.lang.String address) {
   this.address = address;
 }

 public void setChildren(List<Child> children) {
   this.children = children;
 }

 public List<Child> getChildren() {
   return children;
 }
 
 public void writeData() {
   System.out.println("pesel=" + getPesel());
   System.out.println("firstName=" + getFirstName());
   System.out.println("lastName=" + getLastName());
   System.out.println("address=" + getAddress());
   if (getChildren() != null) {
     System.out.println("Number of children: " + getChildren().size());
   }
 }  
}

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email
©2008 junlu.com - Jax Systems, LLC, U.S.A.