Before looking into this in more detail, I have got a question: you seem
to have an XML schema. Why don't you generate the Java classes (and the
corresponding descriptor classes) from the XML schema (using the XML
code generator) and let Castor take care of the rest ?
I have never understood why somebody would want to use a mapping file
when there's an XML schema available.
Werner
Alatalo, Antoni wrote:
Hi,
Here is all in nutshell:
SCHEMA:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/foo"
xmlns:tns="http://example.com/foo" elementFormDefault="qualified">
<complexType name="fooType">
<sequence>
<element name="children" minOccurs="0">
<complexType>
<sequence>
<element name="child"
type="tns:fooType"
maxOccurs="unbounded">
</element>
</sequence>
</complexType>
</element>
</sequence>
<attribute name="name"></attribute>
</complexType>
<element name="foo" type="tns:fooType"></element>
</schema>
CASTOR
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<class name="com.example.Foo" auto-complete="false">
<map-to xml="foo" ns-uri="http://example.com/foo"
ns-prefix="foo" />
<field name="name" type="java.lang.String">
<bind-xml name="name" node="attribute" />
</field>
<field name="children" collection="collection"
type="com.example.Foo" container="false">
</field>
</class>
</mapping>
BEAN
package com.example;
import java.util.Collection;
public class Foo {
private String name;
private Collection<Foo> children;
public Foo(){}
public Foo(String name){
setName(name);
}
public Collection<Foo> getChildren() {
return children;
}
public void setChildren(Collection<Foo> children) {
this.children = children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
TESTCASE:
package com.example;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
import java.util.HashSet;
import junit.framework.TestCase;
import org.exolab.castor.xml.XMLContext;
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.Marshaller;
import org.exolab.castor.xml.ValidationException;
import com.example.Foo;
public class CastorFooTest extends TestCase {
Mapping mapping = null;
XMLContext context = null;
public void setUp() throws IOException, MappingException{
mapping = new Mapping();
mapping.loadMapping("src/META-INF/castor/foo-castor.xml");
context = new XMLContext();
context.addMapping(mapping);
}
public void testMarhallFoo() throws IOException, MappingException,
MarshalException, ValidationException{
Marshaller marshaller = context.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.setWriter(writer);
marshaller.setMarshalAsDocument(false);
marshaller.setMapping(mapping);
marshaller.setSuppressNamespaces(false);
marshaller.marshal(getFoo());
assertEquals(expected, writer.getBuffer().toString());
}
public void testUnMarhallFoo() throws IOException, MappingException,
MarshalException, ValidationException{
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader reader = new StringReader(expected);
Foo result = (Foo)unmarshaller.unmarshal(reader);
Foo exp = getFoo();
assertEquals(exp.getName(), result.getName());
}
private Foo getFoo(){
Foo foo = getFoo("");
Collection<Foo> children = new HashSet<Foo>();
children.add(getFoo("2"));
children.add(getFoo("1"));
foo.setChildren(children);
return foo;
}
private Foo getFoo(String s){
Foo foo = new Foo("foo".concat(s));
return foo;
}
private String expected = "<foo:foo xmlns:foo=\"http://example.com/foo\" name=\"foo\"><foo:children><foo:foo
name=\"foo1\"></foo:foo><foo:foo name=\"foo2\"></foo:foo></foo:children></foo:foo>";
}
Expected output
<?xml version="1.0" encoding="UTF-8"?>
<foo:foo name="foo" xmlns:foo="http://example.com/foo">
<foo:children>
<foo:foo name="foo1"/>
<foo:foo name="foo2"/>
</foo:children>
</foo:foo>
OUTPUT I GET:
<?xml version="1.0" encoding="UTF-8"?>
<foo:foo name="foo" xmlns:foo="http://example.com/foo">
<children> - ERROR NUMBER 1
<foo:foo name="foo1">
<children /> - ERROR NUMBER 2
</foo:foo>
<foo:foo name="foo2">
<children /> - ERROR NUMBER 2
</foo:foo>
</children> - ERROR NUMBER 1
</foo:foo>
Output I get isn't valid:
1. <children> element must have foo prefix as all others: <foo:children>
2. there must not be empty <children/> element at all
Regards.
Antoni
-----Original Message-----
From: Werner Guttmann [mailto:[EMAIL PROTECTED]
Sent: 9. huhtikuuta 2008 15:13
To: [email protected]
Subject: Re: [castor-user] How to use hierarchical objects with wrapper
Alatalo, Antoni wrote:
Hi,
I have Object A that has Collection of Objects A. Normal hierarchy
structure.
I got to work it almost well except two details.
The collection is wrapped. It should look like this Lets name
namespace like bar <bar:foo name="1">
<bar:children>
<bar:foo name="2*"*/>
<bar:foo name="3"/>
</bar:children>
</bar:foo>
The problem is that chioldren while marhalling doen't get bar prefix.
This is output after marshalling
<bar:foo name="1">
<children>
<bar:foo name="2*"*>
<children/>
</bar:foo>
<bar:foo name="3">
<children/>
</bar:foo>
</children>
</bar:foo>
How can i configure mapper to use prefix with "children" wrapper?
Without this xml is not valid!
What version of Castor are you using ? And what does your mapping for the
relevant classes look like ? It looks like you are using the locations
attribute on the field mapping for the 'bar' children ?
Teh next thing is empty collection. How can i configure mapper to
exclude empty or null not required values?
Can you show us some sample XML that highlights your problem ? It just isn't
fully clear to me what the problem is.
Thank you
*Antoni Alatalo
*Ohjelmistoasiantuntija
Kuntatoimiala, sosiaalitoimi
_________________________________________
*Logica **- Releasing your potential
*Karvaamokuja 2
PL 38
00381 Helsinki
Vaihde: 010 302 010
Suora: 040 583 1097
[EMAIL PROTECTED]
_www.logica.fi_ <http://www.logicacmg.fi> WM-datan nimi on nyt
*Logica.*
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email