Simple situation, I have a bean with a mapping file (Bean was not generated
by Castor source gen):
public class SimpleBean {
private id;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
I have a collection of these beans in a vector and Have created a new Class
(called BeanStore) which holds my collection ready for Marshalling:
public class BeanStore {
private Vector store;
public void setStore(Vector v) {
this.store = v;
{
public Vector getStore() {
return this.store;
}
}
I created this class cause I couldn't find out how to Unmarshal an Array of
my Bean:
SimpleBean[] beans = Unmarshaller.unmarshal(?, new StringReader(xmlString));
Using a mapping file and the BeanStore seemed to fix this problem:
JSP:
//set up mapping file
Mapping mapping = new Mapping(getClass().getClassLoader());
InputSource is = new
InputSource(application.getResourceAsStream("/WEB-INF/classes/tools/mappings
/SimpleBeanStoreMapping.xml"));
mapping.loadMapping(is);
//setup a String writer to write our xmlString to
StringWriter strwrt = new StringWriter();
//setup the Marshaller and it's mapping
Marshaller mslr = new Marshaller(strwrt);
mslr.setMapping(mapping);
//populate Beanstore etc...
BeanStore bs = new BeanStore();
bs.setStore(vectorOfSimpleBeans);
//create xmlString
String xmlString = null;
mslr.marshall(bs, strwrt);
xmlString = strwrt.toString();
//up to here it works fine
out.println(xmlString);
//set up an Unmarshaller wiith the mapping
Unmarshaller unmslr = new Unmarshaller(mapping);
//Unmarshall the XML to our class...
BeanStore bs = unmslr.unmarshal(BeanStore.class, new
StringReader(xmlString));
at least so I thought. Running the above Unmarshalling code with a mapping
file throws an exception:
org.xml.sax.SAXException: Parsing Error : Premature end of file.
Line : 1
Column : 1
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:555)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:487)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:627)
at org.apache.jsp.data_jsp._jspService(data_jsp.java:91)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
Now I'm not sure if this is because of the mapping file or the XML string.
Mapping.xml:
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version
1.0//EN" "http://castor.exolab.org/mapping.dtd">
<mapping>
<description>Maps a vector of SimpleBeans to a real Vector</description>
<class name="tools.BeanStore">
<field name="Store" type="beans.SimpleBean" collection="vector">
<bind-xml name="SimpleBean"/>
</field>
</class>
<class name="SimpleBean">
<field name="id" type="integer" />
<field name="name" type="java.lang.String" />
</class>
</mapping>
Do I need to reset the Mapping? or is there a problem using the StringReader
to Unmarshall?
Regards,
Anthony
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.509 / Virus Database: 306 - Release Date: 12/08/2003
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev