Hi,
For the code below has anyone tried to
replace the DocumentHandler (which has been deprecated) with ContentHandler? I
get a SAX.Parser Error as mentioned below.
DocumentBuilderFactory mDocFact = DocumentBuilderFactory.newInstance();
DocumentBuilder mDocBuilder = mDocFact.newDocumentBuilder();
Document mDocument = mDocBuilder.newDocument();
// Load the Mapping XML
Mapping mMap = new Mapping();
mMap.loadMapping("ChartDataMapping.xml");
// Create output format
OutputFormat format = new OutputFormat(mDocument,"UTF-8", true);
// Define the names of the XML elements to put
String[] mCData = {"data"};
format.setCDataElements(mCData);
format.setNonEscapingElements(mCData);
// Create the serializer
Writer mWriter = new StringWriter();
XMLSerializer serializer = new XMLSerializer(mWriter, format);
ContentHandler handler = serializer.asContentHandler();
// Create the marshaller
Marshaller mMarshaller = new Marshaller(handler);
mMarshaller.setMapping(mMap);
mMarshaller.setSuppressXSIType(true);
mMarshaller.marshal(mChartDataInfo);
When I use ContentHandler, I get the
following error:
#endElement: class
#endElement: mapping
org.xml.sax.SAXException: System property org.xml.sax.parser
not specified
at org.xml.sax.helpers.ParserAdapter.<init>(Unknown Source)
at org.exolab.castor.xml.Marshaller.<init>(Marshaller.java:334)
at ChartDataXML.main(ChartDataXML.java:92)
I have read that it can be done by setting
JAVA_OPTS in the system properties. But, is there a way to fix it through code?
Thanks,
Sarita
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
Sent: Tuesday, January 04, 2005
10:43 AM
To: Chandrasekharan, Sarita
Cc: [email protected]
Subject: RE: [castor-user] Castor
- CDATA tags in Mapping XML
// Get mapping
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
String[] cdata = {"fragment"};
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>
- <chart>
- <chart_html_fragments>
- <fragment
appearance_name="TFLINBAR" is_currently_selected="true" link_name="Line-BAR">
<![CDATA[charthtml]]>
</fragment>
</chart_html_fragments>
</chart>
</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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~