Hi Adam,

There are a couple bugs open on the location attribute already, I think
the following covers this situation that you describe:

http://bugzilla.exolab.org/show_bug.cgi?id=1516

Since that bug is lacking a test case, can you add your test case to the
bug, and put yourself in the cc list so that you can be notified of any
changes to the bug report.

Thanks,

--Keith


Adam Murray wrote:
> 
> I'm going to give a working example and then describe my problem.
> 
> Consider the following xml and code:
> ---------------------------------------------------------------
> testData.xml:
> <Tag>
>     <Data attr="attrValue">ElementValue</Data>
> </Tag>
> 
> testMapping.xml:
> <mapping>
>     <class name="test.Test">
>         <map-to xml="Tag"/>
>         <field name="data" type="test.Data">
>             <bind-xml name="Data"/>
>         </field>
>     </class>
>     <class name="test.Data">
>         <map-to xml="Data"/>
>         <field name="attribute" type="string">
>             <bind-xml name="attr" node="attribute"/>
>         </field>
>         <field name="element" type="string">
>             <bind-xml node="text"/>
>         </field>
>     </class>
> </mapping>
> 
> test/Data.java
> package test;
> public class Data {
>     private String attribute;
>     private String element;
>     public String getAttribute() { return attribute; }
>     public void setAttribute(String attribute) { this.attribute =
> attribute; }
>     public String getElement() { return element; }
>     public void setElement(String element) { this.element = element; }
> }
> 
> test/Test.java
> package test;
> import org.exolab.castor.mapping.Mapping;
> import org.exolab.castor.xml.Unmarshaller;
> import org.xml.sax.InputSource;
> import java.io.FileReader;
> public class Test {
>     private Data data;
>     public Data getData() { return data; }
>     public void setData(Data data) { this.data = data; }
> 
>     public static void main(String args[]) {
>         try {
>             Mapping mapping = new Mapping();
>             mapping.loadMapping("testMapping.xml");
>             Unmarshaller unmar = new Unmarshaller(mapping);
>             Test t = (Test)unmar.unmarshal(new InputSource(new
> FileReader("testData.xml")));
>             System.out.println("attribute: " +
> t.getData().getAttribute());
>             System.out.println("element: " + t.getData().getElement());
>         }
>         catch(Exception e) {e.printStackTrace(); }
>     }
> }
> 
> ---------------------------------------------------------------
> 
> This will successfully unmarshal the xml in question and print
> attribute: attrValue
> element: ElementValue
> as expected.
> 
> Here's my problem. I'd like to not have to use the seperate Data class.
> This pattern appears a lot in the xml I'm unmarshalling and I would have
> a ton of almost pointless little classes like Data. There are many
> similar situations where I've successfully unmarshalled everything into
> one class using the location attribute of the <bind-xml> tags in my
> mapping. So I would expect to rewrite my mapping as follows:
> <mapping>
>     <class name="test.Test">
>         <map-to xml="Tag"/>
>         <field name="attribute" type="string">
>             <bind-xml name="attr" location="Data" node="attribute"/>
>         </field>
>         <field name="element" type="string">
>             <bind-xml location="Data" node="text"/>
>         </field>
>     </class>
> </mapping>
> 
> and move all the fields/getters/setters from the Data class back into
> the Test class:
> 
> public class Test {
>     private String attribute;
>     private String element;
>     public String getAttribute() { return attribute; }
>     public void setAttribute(String attribute) { this.attribute =
> attribute; }
>     public String getElement() { return element; }
>     public void setElement(String element) { this.element = element; }
> 
>     public static void main(String args[]) {
>         try {
>             Mapping mapping = new Mapping();
>             mapping.loadMapping("testMapping.xml");
>             Unmarshaller unmar = new Unmarshaller(mapping);
>             Test t = (Test)unmar.unmarshal(new InputSource(new
> FileReader("testData.xml")));
>             System.out.println("attribute: " + t.getAttribute());
>             System.out.println("element: " + t.getElement());
>         }
>         catch(Exception e) {e.printStackTrace(); }
>     }
> }
> 
> But this doesn't work: element will now be null. Which might be because
> name isn't specified for the last <bind-xml> tag. So I tried specifying
> name and removing location:
> <bind-xml name="Data" node="text"/>
> 
> But it still doesn't work. However, if I change text to element:
> <bind-xml name="Data" node="element"/>
> 
> Now element is correct but attribute is null! It seems that when you
> have a node with some attributes that contains only CDATA, you can
> access either the CDATA or the attributes, but not both (unless you use
> the nested class approach originally described).
> 
> Is this a castor bug or am I doing something wrong? Maybe there's
> another mapping approach for this xml pattern?
> 
> FYI I've encountered this situation but with nested tags instead of
> CDATA:
> <Tag>
>     <Data attr="attrValue">
>         <Value>ElementValue</Value>
>     </Data>
> </Tag>
> 
> And never had problems with the attributes being null. Here I could use
> a mapping like:
> <mapping>
>     <class name="test.Test">
>         <map-to xml="Tag"/>
>         <field name="attribute" type="string">
>             <bind-xml name="attr" location="Data" node="attribute"/>
>         </field>
>         <field name="element" type="string">
>             <bind-xml name="Value" location="Data" node="element"/>
>         </field>
>     </class>
> </mapping>
> 
> Unfortunately in my current situation I cannot change the xml format to
> use nested tags so this is not an option.
> 
> --
> Adam Murray <[EMAIL PROTECTED]>
> Fortify Software
> 
> -----------------------------------------------------------
> If you wish to unsubscribe from this mailing, send mail to
> [EMAIL PROTECTED] with a subject of:
>         unsubscribe castor-dev

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to