• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mapping Java fields to attribute and element

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having trouble getting castor to do the following. I have two fields in a java bean, and want to map both fields to one XML element, one being an attribute and the other being content withing the element.

e.g. <Test att="anAttribute">SomeText</Test>

After searching the net I have not been able to get any other answers or examples on how you would do this.

I put together an example.

I have a java bean, like the following.

public class Test implements Serializable {
private String attribute;
private String element;

public Test(){}

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; }
}

and a mapping file using the following.

<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<class name="play.Test">
<map-to xml="Test"/>
<field name="attribute" type="string">
<bind-xml name="Attribute" node="attribute" location="SomeNode"/>
</field>
<field name="element" type="string">
<bind-xml name="SomeNode" node="element" />
</field>
</class>
</mapping>

The XML that I want to generate is like the following. and it seems I can get just about every other permutation other than that.

<?xml version="1.0" encoding="UTF-8"?>
<Test>
<SomeNode Attribute="anAttribute">anElement</SomeNode>
</Test>

With th given mapping file I am getting out the following.

<?xml version="1.0" encoding="UTF-8"?>
<Test>
<SomeNode>anElement</SomeNode>
<SomeNode Attribute="anAttribute"/>
</Test>

I am using castor version 0.9.5.3.

Any help would be appreciated.
 
Ben Storey
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I upgraded to 1.0.2 and this problem went away, using the same mapping file.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"benjipete", welcome to the JavaRanch.

We're pleased to have you here, but we do require members to have valid display names.

Display names must be at least two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names will be deleted.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic