on 07-31-2008 11:59 AM
Hi Guys,
I need to call JDBC adapter so that I need to send the complete source XML payload as a string in just one element and not mapping each element with input variables of the store procedure.
So i developed the java mapping like this
import java.io.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
public class Create_JDBC_Structure
extends DefaultHandler
implements StreamTransformation {
public void setParameter(Map map) {
}
public void execute(InputStream iStream, OutputStream oStream)
throws StreamTransformationException {
// TODO Auto-generated method stub
this.out = oStream;
try {
DefaultHandler handler = this;
SAXParserFactory factory =
SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(iStream, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
strbfr_FirstFields.append("<Statement>");
strbfr_FirstFields.append("<ref_bod_put action=\"\">");
strbfr_FirstFields.append("<table></table>");
strbfr_FirstFields.append("<param1 type=\"\"></param1>");
strbfr_FirstFields.append("<param2 type=\"\"></param2>");
strbfr_FirstFields.append("<param3 type=\"\"></param3>");
strbfr_FirstFields.append("<param4 type=\"\"></param4>");
strbfr_FirstFields.append("<param5 type=\"\"></param5>");
strbfr_FirstFields.append("<param6 type=\"\"></param6>");
strbfr_FirstFields.append("<param7 type=\"\"></param7>");
strbfr_FirstFields.append("<param8>");
}
public void startElement(
String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
ifNewRec = true;
}
if (ifNewRec) {
if (!(qName.equals("Records"))) {
strbfr_LastField.append("<");
strbfr_LastField.append(qName);
if (qName.equals("SITELEVEL")) {
strbfr_LastField.append(" " +
atts.getQName(0) + "=");
strbfr_LastField.append(atts.getValue(0));
}
if (qName.equals("DATETIME")) {
strbfr_LastField.append(" " +
atts.getQName(0) + "=");
strbfr_LastField.append(atts.getValue(0));
}
if (qName.equals("AMOUNT")) {
strbfr_LastField.append(" " +
atts.getQName(0) + "=");
strbfr_LastField.append(atts.getValue(0));
}
if (qName.equals("DATE_TIME")) {
strbfr_LastField.append(" " +
atts.getQName(0) + "=");
strbfr_LastField.append(atts.getValue(0));
}
if (qName.equals("AMOUNT1")) {
strbfr_LastField.append(" " +
atts.getQName(0) + "=");
strbfr_LastField.append(atts.getValue(0));
}
if (qName.equals("OPERAMT")) {
strbfr_LastField.append(" " +
atts.getQName(0) + "=");
strbfr_LastField.append(atts.getValue(0));
}
if (qName.equals("QUANTITY")) {
strbfr_LastField.append(" " +
atts.getQName(0) + "=");
strbfr_LastField.append(atts.getValue(0));
}
if (qName.equals("CCA_GLACCT")) {
strbfr_LastField.append(" " +
atts.getQName(0) + "=");
strbfr_LastField.append(atts.getValue(0));
}
strbfr_LastField.append(">");
} else {
//
strbfr_LastField.append("<!DOCTYPE SYNC_ITEM_005>");
//
strbfr_LastField.append("<");
//
strbfr_LastField.append("SYNC_ITEM_005");
//
strbfr_LastField.append(">");
}
}
}
public void characters(char[] arg0, int arg1, int arg2)
throws SAXException {
if (ifNewRec) {
strbfr_LastField.append(data);
}
}
public void endElement(String namespaceURI, String localName, String
qName)
throws SAXException {
// echoText();
if (ifNewRec) {
if (!(qName.equals("Records"))) {
strbfr_LastField.append("</");
strbfr_LastField.append(qName);
strbfr_LastField.append(">");
} else {
//
strbfr_LastField.append("</");
//
strbfr_LastField.append("SYNC_ITEM_005");
//
strbfr_LastField.append(">");
}
}
if (qName.equals("Records")) {
strbfr_LastField.append("</param8>");
strbfr_LastField.append("</ref_bod_put>");
strbfr_LastField.append("</Statement>");
ifNewRec = false;
}
}
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
outputxml.append("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>");
outputxml.append(
"<ns0:JDBC_MT
xmlns:ns0=\"http://ccamatil.com/amatil_c_ccaml/ptp/\">");
outputxml.append(strbfr_LastField.toString());
outputxml.append("</ns0:JDBC_MT>");
print(outputxml.toString());
}
private void print(String str) {
try {
out.write(str.getBytes());
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
String data = null;
private OutputStream out;
private StringBuffer outputxml = new StringBuffer();
///////////////////////////
private boolean ifNewRec = false;
private int int_No_Rec;
private StringBuffer strbfr_LastField = new StringBuffer();
private StringBuffer strbfr_FirstFields = new StringBuffer();
private Hashtable ht = new Hashtable();
////////////////////////////
}
so what are the changes i have to make to get whole payload as a sting
Thans In advance.
Rishi
Hi Rishi,
your coding will be much more easier if you use DOM instead of SAX.
The method
getDocumentElement.toString
should give you the whole document.
regards mario
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
public void execute(InputStream in, OutputStream out) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser;
try {
parser = factory.newDocumentBuilder();
// Erstellen eines DOM-Dokuments durch parsen des InputStream
Document domDOC = parser.parse(in);
// Ausgabe des Dokuments
System.out.println(domDOC.getDocumentElement());
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}// public void execute(InputStream in, OutputStream out) {
Regards Mario
Edited by: Mario Müller on Jul 31, 2008 7:13 AM
Edited by: Mario Müller on Jul 31, 2008 7:13 AM
Hi Rishik ,
the coding I have posted is the method execute that executes the mapping.
just take the coding I have posted and replace it with your coding.
First try to understand the coding. Paste it in your JAVA-IDE (I guess you use eclipse) and debug it to see whats happening.
Ask colleagues for assistance.
Regards Mario
Hi,
You can follow this procedure also
BufferedReader in = new BufferedReader(new InputStreamReader(inp));
StringBuffer buffer = new StringBuffer();
String line="";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
String sourcexml=buffer.toString();
Thanks,
RamuV
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
You have to check there is a method called toString() or xmlText() method in DOM or SAX Api. Go through DOM and SAX APIS. As i know if you use XML Beans you will have method called XMLObject.xmlText(); it convert your complete XML into a string.
Thanks,
RamuV
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
83 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.