aspose file tools*
The moose likes XML and Related Technologies and the fly likes Mapping Java fields to attribute and element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Mapping Java fields to attribute and element" Watch "Mapping Java fields to attribute and element" New topic
Author

Mapping Java fields to attribute and element

Ben Storey
Greenhorn

Joined: Feb 28, 2005
Posts: 5
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

Joined: Feb 28, 2005
Posts: 5
I upgraded to 1.0.2 and this problem went away, using the same mapping file.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

"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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping Java fields to attribute and element