File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Mapping Java fields to attribute and element

Ben Storey

Joined: Feb 28, 2005
Posts: 5

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"
<class name="play.Test">
<map-to xml="Test"/>
<field name="attribute" type="string">
<bind-xml name="Attribute" node="attribute" location="SomeNode"/>
<field name="element" type="string">
<bind-xml name="SomeNode" node="element" />

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"?>
<SomeNode Attribute="anAttribute">anElement</SomeNode>

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

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

I am using castor version

Any help would be appreciated.
Ben Storey

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

Joined: Oct 14, 2005
Posts: 19973

"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:
subject: Mapping Java fields to attribute and element
It's not a secret anymore!