Mapping mapping = new Mapping();
// The file containing the mapping here ex. test
mapping.loadMapping(test);
// Create output format
OutputFormat format = new OutputFormat(Method.XML, "UTF-8", true);
// Define the names of the XML elements to put
format.setCDataElements(cdata);
format.setNonEscapingElements(cdata);
// Create the serializer
XMLSerializer serializer = new XMLSerializer(writer, format);
// Create the document handler
DocumentHandler handler = serializer.asDocumentHandler();
// Create the marshaller
Marshaller marshaller = new Marshaller(handler);
marshaller.setMapping(mapping);
// Do it!
marshaller.marshal(object); // The object you wanna marshal
Here you need to modify the mapping file also it would be like...
<field name="cData">
<bind-xml name="fragment" node="element"
location="chart/chart_html_fragments"/> </field>
<field name="appearanceName" >
<bind-xml name="appearance_name"
node="attribute"
location="chart/chart_html_fragments/fragment"/>
</field>
<field name="isCurrentlySelected"> <bind-xml name="is_currently_selected"
node="attribute"
location="chart/chart_html_fragments/fragment"/>
</field>
<field name="linkName"> <bind-xml name="link_name"
node="attribute"
location="chart/chart_html_fragments/fragment"/>
</field> Thanks, Snehal
-----Original Message-----
From: Chandrasekharan, Sarita [mailto:[EMAIL PROTECTED]
Sent: Tuesday, January 04, 2005 8:13 PM
To: [email protected]
Subject: [castor-user] Castor - CDATA tags in Mapping XML
Hi,
I have a class which has four attributes in it as follows. I want the last attribute cData which is a string to be embedded within CDATA tags . How do I do this in the Mapping XML which the Marshall process uses? I have sent the java class which is the object, the class which sets the value for the object, the mapping XML and the output expected. In my example I get the cData attribute wrapped around element CDATA, but I want it the actual CDATA tags around the string . Any help is appreciated.
public class ChartDataInfo
{
//attributes
protected String appearanceName;
protected String linkName;
protected boolean isCurrentlySelected;
protected String cData;
/** Creates a new instance of ChartDataInfo */
public ChartDataInfo() {
}
public String getAppearanceName() { return appearanceName; }
public String getLinkName() { return linkName; }
public boolean getIsCurrentlySelected() { return isCurrentlySelected; }
public String getCData() { return cData; }
public void setAppearanceName(String pValue) { appearanceName = pValue; }
public void setLinkName(String pValue) { linkName = pValue; }
public void setIsCurrentlySelected(boolean pValue) { isCurrentlySelected = pValue; }
public void setCData(String pValue) { cData = pValue; }
}
CLASS which sets the value for ChartDataInfo class
public class ChartDataXML
{
public static void main(String[] argv) {
// Create a new MetaDataInfo
ChartDataInfo mChartDataInfo = new ChartDataInfo();
mChartDataInfo.setAppearanceName("TFLINBAR");
mChartDataInfo.setLinkName("Line-BAR");
mChartDataInfo.setIsCurrentlySelected(true);
mChartDataInfo.setCData("charthtml");
// write it out as XML
String encoding = "ISO-8859-1";
String mMapping = "ChartDataMapping.xml";
StringBuffer mStrBuf = MarshalBean.generateBOXML(mMapping, mChartDataInfo);
System.out.println(mStrBuf);
}
}
Mapping FILE
<!DOCTYPE databases PUBLIC
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd">
<mapping>
<description>ChartData Mapping</description>
<class name="ChartDataInfo">
<map-to xml="charts"/>
<field name="appearanceName" >
<bind-xml name="appearance_name" node="attribute" location="chart/chart_html_fragments/fragment"/>
</field>
<field name="isCurrentlySelected">
<bind-xml name="is_currently_selected" node="attribute" location="chart/chart_html_fragments/fragment"/>
</field>
<field name="linkName">
<bind-xml name="link_name" node="attribute" location="chart/chart_html_fragments/fragment"/>
</field>
<field name="cData">
<bind-xml name="CData" node="element" location="chart/chart_html_fragments/fragment"/>
</field>
</class>
</mapping>
Expected output
<?xml version="1.0" encoding="UTF-8" ?>
- <charts>
</charts>
Thanks,
Sarita
MASTEK
"Making a valuable difference"
Mastek in NASSCOM's 'India Top 20' Software Service Exporters List.
In the US, we're called MAJESCO
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Opinions expressed in this e-mail are those of the individual and not that of Mastek Limited, unless specifically indicated to that effect. Mastek Limited does not accept any responsibility or liability for it. This e-mail and attachments (if any) transmitted with it are confidential and/or privileged and solely for the use of the intended person or entity to which it is addressed. Any review, re-transmission, dissemination or other use of or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. This e-mail and its attachments have been scanned for the presence of computer viruses. It is the responsibility of the recipient to run the virus check on e-mails and attachments before opening them. If you have received this e-mail in error, kindly delete this e-mail from all computers.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-user
