Alatalo, Antoni wrote:
All right it works. I did misundestund about container element. After removing it all works fine. Thank you very much. As you see the result is very clean. Only two files and POJO does not have any dependencies on Castor. Very nice. I think you can place this example to the Cator site to provide simple Hierarchy functionality.
Would you mind - given the time I have spent on helping you - coming up with a well-structured HOW-TO document for the XML part of Castor ?
Werner
Thank you very much Antoni -----Original Message----- From: Werner Guttmann [mailto:[EMAIL PROTECTED] Sent: 11. huhtikuuta 2008 12:30 To: [email protected] Subject: Re: [castor-user] How to use hierarchical objects with wrapper Using your test case (the one supplied below), I get the following output after marshalling:<foo:foo xmlns:foo="http://example.com/foo" name="foo"> <foo:children> <foo:foo name="foo1"/> <foo:foo name="foo2"/> </foo:children> </foo:foo>Which - as far as I can tell - is what you wanted, correct ? Werner Alatalo, Antoni wrote:Hi, Thank you for reply. Still doesn't work MAPPING: <?xml version="1.0"?> <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor MappingDTD Version 1.0//EN" "http://castor.org/mapping.dtd"> <mapping> <class name="com.example.Foo" auto-complete="false"> <map-toxml="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"> <bind-xml name="foo" location="children"/> </field> </class> </mapping>Gets the output <foo:foo xmlns:foo="http://example.com/foo" name="foo"> <foo> <foo:foo name="foo1"> <foo /> </foo:foo> <foo:foo name="foo2"> <foo /> </foo:foo> </foo> </foo:foo> And should be: <foo:foo name="foo" xmlns:foo="http://example.com/foo"> <foo:children> <foo:foo name="foo1" /> <foo:foo name="foo2" /> </foo:children> </foo:foo> The location attribute changes children name to foo, nothing else. Still name of element is without namespace prefix. Another empty <children/> (or with location <foo/>) element exists. How it can be removed? Thank you. Antoni -----Original Message----- From: Werner Guttmann[mailto:[EMAIL PROTECTED] Sent: 11. huhtikuuta 2008 11:41 To: [email protected] Subject: Re: [castor-user] How to usehierarchical objects with wrapper Actually, please ignore my last comment re: the use of the 'container' attribute. It won't work in your case, and switching to the use of the location attribute is the correct way forward. Regards Werner Werner Guttmann wrote:Hi Antoni, looking at the missing namespace prefix on the children element, I changed the mapping as follows (and introduced a location attribute on the <bind-xml> definition for the children field mapping. That did it for me.<mapping> <class name="xml.children.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="xml.children.Foo"> <bind-xml name="foo" location="children" /> </field> </class> </mapping> I will have another look at the use of the container attribute, as it looks like a bug to me .... but I think that the solution shown should satisfy you. Werner Alatalo, Antoni wrote:To continue this conversation question once again: Is there a way to have xsd and POJO like I have (or yours modified xsd) and get the ouput I expect by using castors mapping file? 1. children with namespace prefix 2. empty children are not marhaled? Thank you a lot for your requests Antoni -----Original Message----- From: Alatalo, Antoni [mailto:[EMAIL PROTECTED] Sent: 10. huhtikuuta 2008 13:49 To: [email protected] Subject: RE: [castor-user] How to use hierarchical objects with wrapperHi, I've did generate code and it's not right way for me. Enstead of one domain class I get six classes. From simple 20line class I get something too complex.As I mentioned before I want to use same domain structure independent from the technologies. In the other words nohibernate, castor or axis should be present in the model. Of course here can be three different domain models, one is my own, second generated by castor, third database layer model. But it requers mappings between models and is not what we are looking.The ideal is something like castors mapping file. It's a glue between java class and xsd schema. Only thing is to get it workin every situation. Regards. Antoni -----Original Message----- From: Werner Guttmann [mailto:[EMAIL PROTECTED] Sent: 10. huhtikuuta 2008 13:23 To: [email protected] Subject: Re: [castor-user] How to use hierarchical objects with wrapper 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=typeEverything 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"?> <schemaxmlns="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> <elementref="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"> <complexTypename="fooType"> <sequence> <element name="children"minOccurs="0"> <complexType> <sequence> <element name="child" type="tns:fooType"maxOccurs="unbounded"> </element> </sequence> </complexType> </element> </sequence> <attributename="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> <classname="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> <fieldname="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; importorg.exolab.castor.xml.XMLContext; import org.exolab.castor.mapping.Mapping; importorg.exolab.castor.mapping.MappingException; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.Unmarshaller; importorg.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()); } privateFoo 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 = newFoo("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:fooname="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:fooname="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 nimion 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 -------------------------------------------------------------------- - 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--------------------------------------------------------------------- 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

