I have returned the following String strFees720
which equals :
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Fees720>
<errorNode>
<errorCode>0</errorCode>
<errorMsg/>
<errorSev/>
</errorNode>
<forwardScroll> 0093101200001117 0</forwardScroll>
<backwardScroll> </backwardScroll>
<Field name="ACCID"><Value>93101200001034</Value></Field>
<Field name="FPERIOD"><Value>2C</Value></Field>
<Field name="STDATE"><Value>13 DEC 2012</Value></Field>
<Field name="ENDATE"><Value>13 DEC 2012</Value></Field>
<Field name="ADVDATE"><Value>19 DEC 2012</Value></Field>
<Field name="DBDATE"><Value> F40LA1C </Value></Field>
<Field name="SNAME"><Value>N SINGLETON JG</Value></Field>
<Field name="ACDESC"><Value>OVERDRAFT AAA</Value></Field>
<Field name="TOTFEES"><Value>0</Value></Field>
<Field name="DISCOUNT"><Value>0</Value></Field>
<Field name="NETFEES"><Value>0</Value></Field>
<Field name="CLRD_BAL"><Value>93101200001034</Value></Field>
<Field name="ALLOW_RTE"><Value>0</Value></Field>
<Field name="DISC_RTE"><Value>0</Value></Field>
<Field name="ALLOW_TOT"><Value>0</Value></Field>
<Field name="PCODE"><Value>0000000</Value></Field>
<Field name="DRILLNSC"><Value>0</Value></Field>
<Field name="DIALOG"><Value>9</Value></Field>
<Field name="NUMLINES"><Value>1</Value></Field>
<Field name="TOTLINES"><Value>0</Value></Field>
<Field name="BPCUST"><Value>P</Value></Field>
<Field name="NFEE_EXPDTE"><Value></Value></Field>
<FEEDATA>
<Field name="FCODE"><Value></Value></Field>
<Field name="FEEDETS"><Value></Value></Field>
<Field name="CHRG_TYPE"><Value></Value></Field>
<Field name="DCODE"><Value></Value></Field>
<Field name="VOLUME"><Value>0</Value></Field>
<Field name="RATE"><Value>0</Value></Field>
<Field name="TOTAL"><Value>0</Value></Field>
<Field name="DRILL"><Value></Value></Field>
</FEEDATA>
<GROSSNETFEES>
<Field name="FQUARTER"><Value>06 DEC 12 12 DEC 12</Value></Field>
<Field name="GRSFEE"><Value>611960</Value></Field>
<Field name="NFEE"><Value>611960</Value></Field>
<Field name="FEEDISC"><Value>0</Value></Field>
</GROSSNETFEES>
<GROSSNETFEES>
<Field name="FQUARTER"><Value>01 DEC 12 05 DEC 12</Value></Field>
<Field name="GRSFEE"><Value>64850</Value></Field>
<Field name="NFEE"><Value>64850</Value></Field>
<Field name="FEEDISC"><Value>0</Value></Field>
</GROSSNETFEES>
<GROSSNETFEES>
<Field name="FQUARTER"><Value>08 NOV 12 30 NOV 12</Value></Field>
<Field name="GRSFEE"><Value>15866</Value></Field>
<Field name="NFEE"><Value>15866</Value></Field>
<Field name="FEEDISC"><Value>0</Value></Field>
</GROSSNETFEES>
<GROSSNETFEES>
<Field name="FQUARTER"><Value>01 NOV 12 07 NOV 12</Value></Field>
<Field name="GRSFEE"><Value>636</Value></Field>
<Field name="NFEE"><Value>616</Value></Field>
<Field name="FEEDISC"><Value>20</Value></Field>
</GROSSNETFEES>
<Field name="TOTGRSFEE"><Value>693312</Value></Field>
<Field name="TOTNETFEE"><Value>693292</Value></Field>
</Fees720>
</Response>
I would like to transform this xml string response into a new xml string
as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Fees720>
<errorNode>
<errorCode>0</errorCode>
<errorMsg/>
<errorSev/>
</errorNode>
<forwardScroll> 0093101200001117 0</forwardScroll>
<backwardScroll> </backwardScroll>
<accid>93101200001034</accid>
<fperiod>2C</fperiod>
<stdate>13 DEC 2012</stdate>
<endate>13 DEC 2012</endate>
<advdate>19 DEC 2012</advdate>
<dbdate> F40LA1C </dbdate>
<sname>N SINGLETON JG</sname>
<acdesc>OVERDRAFT AAA</acdesc>
<totfees>0</totfees>
<discount>0</discount>
<netfees>0</netfees>
<clrdbal>93101200001034</clrdbal>
<allowrte>0</allowrte>
<discrte>0</discrte>
<allowtot>0</allowtot>
<pcode>0000000</pcode>
<drillnsc>0</drillnsc>
<dialog>9</dialog>
<numlines>1</numlines>
<totlines>0</totlines>
<bpcust>P</bpcust>
<nfeeexpdte></nfeeexpdte>
<feedata>
<fcode></fcode>
<feedets></feedets>
<chrgtype></chrgtype>
<dcode></dcode>
<volume>0</volume>
<rate>0</rate>
<total>0</total>
<drill></drill>
</feedata>
<grossnetfees>
<fquarter>06 DEC 12 12 DEC 12</fquarter>
<grsfee>611960</grsfee>
<nfee>611960</nfee>
<feedisc>0</feedisc>
</grossnetfees>
<grossnetfees>
<fquarter>01 DEC 12 05 DEC 12</fquarter>
<grsfee>64850</grsfee>
<nfee>64850</nfee>
<feedisc>0</feedisc>
</grossnetfees>
<grossnetfees>
<fquarter>08 NOV 12 30 NOV 12</fquarter>
<grsfee>15866</grsfee>
<nfee>15866</nfee>
<feedisc>0</feedisc>
</grossnetfees>
<grossnetfees>
<fquarter>01 NOV 12 07 NOV 12</fquarter>
<grsfee>636</grsfee>
<nfee>616</nfee>
<feedisc>20</feedisc>
</grossnetfees>
<totgrsfee>693312</totgrsfee>
<totnetfee>693292</totnetfee>
</Fees720>
</Response>
I’m thinking I need to do something along the lines of:
strFees720Transform = XSLTransformer.transform("mynewxsl.xsl", strFees720);
and transform the original xml into the new xml. Any advice on best way to do this please. Thanks
updated with method
private String getServiceValue(String strXMLResponse, String strNode, String strService) throws ServletException{
String strNodeValue = null;
try{
builder = new SAXBuilder(false);
Document doc = builder.build(new InputSource(new StringReader(strXMLResponse)));
Element root = doc.getRootElement();
Element rootchild = root.getChild(strService);
List rootchildren = rootchild.getChildren();
for (int i=0;i <= rootchildren.size() -1;i++)
{
Element el = (Element) rootchildren.get(i);
if(el.getName().equalsIgnoreCase(strNode)){
strNodeValue = el.getText();
}
}
} catch (Exception err) {
logger.error(err.getMessage(), err);
throw new ServletException(err.getMessage());
}
return strNodeValue;
}
The usual advice for “I want to make a few changes to my XML but keep most of it the same” questions is: start with an identity template (which copies the input to output verbatim), then add specific templates for the things you want to change. The identity template looks like
Now to handle the
<Field>elements you want to create a new element whose name is derived from an attribute of the originalFieldThe
translatefunction here will replace all upper-case letters with the equivalent lower-case, and also remove any underscores completely.Finally for the
FEEDATAandGROSSNETFEESyou just need two simple templatesYou could combine these two templates into one using the same
translatetrick if you prefer.As for the actual mechanics of doing the transformation, you need to look at the javax.xml.transform package. Seeing as you have code already to parse an XML string into a JDOM tree and extract the element of interest, I would do the XSL transformation in there. Create a single
Templatesinstance at initialization time and store the reference somewhereNow each time you need to do the transformation you can use this
templates