Hi,
there you are:
**** XML 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="foo">
<sequence>
<element name="children" minOccurs="0">
<complexType>
<sequence>
<element name="foo"
type="tns:foo"
maxOccurs="unbounded">
</element>
</sequence>
</complexType>
</element>
</sequence>
<attribute name="name"></attribute>
</complexType>
<!--
<element name="foo" type="tns:fooType"></element>
-->
</schema>
*** builder properties:
# Selects the Java class mapping for <xsd:element>'s and
<xsd:complexType>'s.
# Legal values are 'element' and 'type'. For 'element' mapping, the source
# generator creates a Java class hierarchy based on elements in the XML
Schema.
# For 'type' mapping, the class hierarchy is based on types in the XML
Schema.
# Default is 'element'.
#
org.exolab.castor.builder.javaclassmapping=type
Everything else is standard, so please generate the classes yourself.
Please note that I changed the XML schema to meet the expected XML
defined in the test case.
Regards
Werner Guttmannb
Alatalo, Antoni wrote:
Please, send me schema you modified and class you are generated
Seems that I send you wrong version of xsd. Here is what I'm using. Only
different is ref
<?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" nillable="false">
<complexType>
<sequence>
<element ref="tns:foo"
maxOccurs="unbounded">
</element>
</sequence>
</complexType>
</element>
</sequence>
<attribute name="name"></attribute>
</complexType>
<element name="foo" type="tns:fooType"></element>
</schema>
Terv.
Antoni
-----Original Message-----
From: Werner Guttmann [mailto:[EMAIL PROTECTED]
Sent: 10. huhtikuuta 2008 12:45
To: [email protected]
Subject: Re: [castor-user] How to use hierarchical objects with wrapper
Just to let you know, after modifying your XML schema slightly, I was able to
pruduce the desired output as follows just by generating classes from the XML
schema given, without having to write a mapping file.
Regards
Werner
Committer, Castor
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
---------------------------------------------------------------------
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