cancel
Showing results for 
Search instead for 
Did you mean: 

java mapping issue

former_member206760
Active Contributor
0 Kudos

Hi

I am following http://www.riyaz.net/blog/xipi-java-mapping-demystified/technology/sap/415/

and have written a sample java mapping code...but am getting the error " XML not welformed " content not allowed in prolog while testing the Operation mapping...

what may be the issue ??

package jmapsax;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import com.sap.aii.mapping.api.StreamTransformation;

import java.io.*;

import java.util.Map;

import javax.xml.parsers.*;

import org.xml.sax.*;

import org.xml.sax.helpers.*;

public class saxmapfinal1 extends DefaultHandler implements StreamTransformation{

private Map map;

private OutputStream out;

private boolean input1 = false;

private boolean input2 = false;

private int number1;

private int number2;

private int addvalue;

private int mulvalue;

private int subvalue;

String lineEnd = System.getProperty("line.separator");

public void setParameter (Map param) {

map = param;

}

public void execute (InputStream in, OutputStream out)

throws com.sap.aii.mapping.api.StreamTransformationException {

DefaultHandler handler = this;

SAXParserFactory factory = SAXParserFactory.newInstance();

try {

SAXParser saxParser = factory.newSAXParser();

this.out = out;

saxParser.parse(in, handler);

}

catch (Throwable t){

t.printStackTrace();

}

}

private void write (String s) throws SAXException{

try{

out.write(s.getBytes());

out.flush();

}

catch (IOException e){

throw new SAXException("I/O error", e);

}

}

public void startDocument () throws SAXException{

write("");

write(lineEnd);

write("");

write(lineEnd);

}

public void endDocument () throws SAXException {

write("");

try { out.flush();

}

catch (IOException e) {

throw new SAXException("I/O error", e);

}

}

public void startElement (String namespaceURI, String sName,

String qName, Attributes attrs) throws SAXException {

String eName = sName;

if ("".equals(eName)) eName = qName;

if(eName.equals("NUMBER1")) input1 = true;

if(eName.equals("NUMBER2")) input2 = true;

}

public void endElement (String namespaceURI, String sName, String qName) throws SAXException {

String eName = sName;

if ("".equals(eName)) eName = qName;

if(eName.equals("NUMBER1")) input1 = false;

if(eName.equals("NUMBER2")) input2 = false;

}

public void characters(char[] chars,int startIndex, int endIndex) throws SAXException {

String dataString =

new String(chars, startIndex, endIndex).trim();

if (input1) {

try {

number1 = Integer.parseInt(dataString);

} catch(NumberFormatException nfe){

}

}

if (input2) {

number2 = Integer.parseInt(dataString);

}

if (input2 == true){

addvalue = number1 + number2;

mulvalue = number1 * number2;

subvalue = number1 - number2;

write("" + addvalue +"");

write(lineEnd);

write("" + mulvalue +"");

write(lineEnd);

write("" + subvalue +"");

write(lineEnd);

}

}

}

Edited by: Tarang Shah on Jun 4, 2011 8:17 PM

Accepted Solutions (1)

Accepted Solutions (1)

rajasekhar_reddy14
Active Contributor
0 Kudos

The output generated by JAVA Mapping not valid XML format and it is not matching with target structure what you have defined for inbound service interface.

first debug JAVA mapping in eclipse or NWDS.

Regards,

Raj

Answers (4)

Answers (4)

former_member206760
Active Contributor
0 Kudos

thanks for your help

Former Member
0 Kudos

Hi,

Replace ur "startDocument", "endDocument" and "characters" method with the code mentioend below:


public void startDocument () throws SAXException{

	write("<ns0:MT_JavaMapping xmlns:ns0=\"http://java/poc2\">");
	write("<Row>");
write("");
write(lineEnd);
write("");
write(lineEnd);

}

public void endDocument () throws SAXException {

	write("</Row>");
	write("</ns0:MT_JavaMapping>");
	
	
write("");
try { out.flush();
}
catch (IOException e) {
throw new SAXException("I/O error", e);
}



public void characters(char[] chars,int startIndex, int endIndex) throws SAXException {

String dataString =
new String(chars, startIndex, endIndex).trim();
if (input1) {
try {
number1 = Integer.parseInt(dataString);
} catch(NumberFormatException nfe){
}
}
if (input2) {
number2 = Integer.parseInt(dataString);
}
if (input2 == true){
addvalue = number1 + number2;
mulvalue = number1 * number2;
subvalue = number1 - number2;
write("<Add>" + addvalue +"</Add>");
write(lineEnd);
write("<Multiply>" + mulvalue +"</Multiply>");
write(lineEnd);
write("<Substract>" + subvalue +"</Substract>");
write(lineEnd);
}

}

Thanks

Amit

rajasekhar_reddy14
Active Contributor
0 Kudos

HI ,

JAVA Mapping output not givingt valid XML Structure,Amit already pointed,try below code first in eclipse then move to PI.

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import com.sap.aii.mapping.api.StreamTransformation;
import java.io.*;
import java.util.Map;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class JavaMapping extends DefaultHandler implements
		StreamTransformation {

	private Map map;
	private OutputStream out;
	private boolean input1 = false;
	private boolean input2 = false;
	private int number1;
	private int number2;
	private int addvalue;
	private int mulvalue;
	private int subvalue;

	String lineEnd = System.getProperty("line.separator");

	public void setParameter(Map param) {

		map = param;

	}

	public void execute(InputStream in, OutputStream out)
			throws com.sap.aii.mapping.api.StreamTransformationException {

		DefaultHandler handler = this;
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser saxParser = factory.newSAXParser();
			this.out = out;
			saxParser.parse(in, handler);
		} catch (Throwable t) {
			t.printStackTrace();
		}

	}

	private void write(String s) throws SAXException {

		try {
			out.write(s.getBytes());
			out.flush();
			System.out.println(out);
		} catch (IOException e) {
			throw new SAXException("I/O error", e);
		}

	}

	public void startDocument () throws SAXException{
		 
		write("<ns0:MT_JavaMapping xmlns:ns0=\"http://java/poc2\">");
		write("<Row>");
	write("");
	write(lineEnd);
	write("");
	write(lineEnd);
	 
	}


	public void endDocument () throws SAXException {
		 
		write("</Row>");
		write("</ns0:MT_JavaMapping>");
		
		
	write("");
	try { out.flush();
	}
	catch (IOException e) {
	throw new SAXException("I/O error", e);
	}
	}
	

	public void startElement(String namespaceURI, String sName, String qName,
			Attributes attrs) throws SAXException {

		String eName = sName;
		if ("".equals(eName))
			eName = qName;
		if (eName.equals("NUMBER1"))
			input1 = true;
		if (eName.equals("NUMBER2"))
			input2 = true;

	}

	public void endElement(String namespaceURI, String sName, String qName)
			throws SAXException {

		String eName = sName;
		if ("".equals(eName))
			eName = qName;
		if (eName.equals("NUMBER1"))
			input1 = false;
		if (eName.equals("NUMBER2"))
			input2 = false;

	}

	public void characters(char[] chars,int startIndex, int endIndex) throws SAXException {
		 
		String dataString =
		new String(chars, startIndex, endIndex).trim();
		if (input1) {
		try {
		number1 = Integer.parseInt(dataString);
		} catch(NumberFormatException nfe){
		}
		}
		if (input2) {
		number2 = Integer.parseInt(dataString);
		}
		if (input2 == true){
		addvalue = number1 + number2;
		mulvalue = number1 * number2;
		subvalue = number1 - number2;
		write("<Add>" + addvalue +"</Add>");
		write(lineEnd);
		write("<Multiply>" + mulvalue +"</Multiply>");
		write(lineEnd);
		write("<Substract>" + subvalue +"</Substract>");
		write(lineEnd);
		}
		 
		}

	public static void main(String[] args) {

		try {

			JavaMapping fec = new JavaMapping();

			FileInputStream in =

			new FileInputStream("D:\\B18\\Raj1.xml");

			OutputStream out = new ByteArrayOutputStream();

			fec.execute(in, out);

		} catch (Exception e) {

			System.out.println(e);

		}

	}
}

rajasekhar_reddy14
Active Contributor
0 Kudos

Hi,

Please test below code in Eclipse and see results,just i have added main method so that you can test easily in Eclipse.

but not sure what logic written in JAVA Mapping

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import com.sap.aii.mapping.api.StreamTransformation;
import java.io.*;
import java.util.Map;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class JavaMapping extends DefaultHandler implements
		StreamTransformation {

	private Map map;
	private OutputStream out;
	private boolean input1 = false;
	private boolean input2 = false;
	private int number1;
	private int number2;
	private int addvalue;
	private int mulvalue;
	private int subvalue;

	String lineEnd = System.getProperty("line.separator");

	public void setParameter(Map param) {

		map = param;

	}

	public void execute(InputStream in, OutputStream out)
			throws com.sap.aii.mapping.api.StreamTransformationException {

		DefaultHandler handler = this;
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser saxParser = factory.newSAXParser();
			this.out = out;
			saxParser.parse(in, handler);
		} catch (Throwable t) {
			t.printStackTrace();
		}

	}

	private void write(String s) throws SAXException {

		try {
			out.write(s.getBytes());
			out.flush();
			System.out.println(out);
		} catch (IOException e) {
			throw new SAXException("I/O error", e);
		}

	}

	public void startDocument() throws SAXException {

		write("");
		write(lineEnd);
		write("");
		write(lineEnd);

	}

	public void endDocument() throws SAXException {

		write("");
		try {
			out.flush();
		} catch (IOException e) {
			throw new SAXException("I/O error", e);
		}

	}

	public void startElement(String namespaceURI, String sName, String qName,
			Attributes attrs) throws SAXException {

		String eName = sName;
		if ("".equals(eName))
			eName = qName;
		if (eName.equals("NUMBER1"))
			input1 = true;
		if (eName.equals("NUMBER2"))
			input2 = true;

	}

	public void endElement(String namespaceURI, String sName, String qName)
			throws SAXException {

		String eName = sName;
		if ("".equals(eName))
			eName = qName;
		if (eName.equals("NUMBER1"))
			input1 = false;
		if (eName.equals("NUMBER2"))
			input2 = false;

	}

	public void characters(char[] chars, int startIndex, int endIndex)
			throws SAXException {

		String dataString = new String(chars, startIndex, endIndex).trim();
		if (input1) {
			try {
				number1 = Integer.parseInt(dataString);
			} catch (NumberFormatException nfe) {
			}
		}
		if (input2) {
			number2 = Integer.parseInt(dataString);
		}
		if (input2 == true) {
			addvalue = number1 + number2;
			mulvalue = number1 * number2;
			subvalue = number1 - number2;
			write("" + addvalue + "");
			write(lineEnd);
			write("" + mulvalue + "");
			write(lineEnd);
			write("" + subvalue + "");
			write(lineEnd);
		}

	}
	public static void main(String[] args) {

		try {

			JavaMapping fec = new JavaMapping();

			FileInputStream in =

			new FileInputStream("D:\\B18\\Raj1.xml");

			OutputStream out = new ByteArrayOutputStream();

			fec.execute(in, out);

		} catch (Exception e) {

			System.out.println(e);

		}

	}
}

former_member200962
Active Contributor
0 Kudos
have written a sample java mapping code...but am getting the error " XML not welformed " content not allowed in prolog 
while testing the Operation mapping

You using PI7.1 or higher? The code in the blog seems to be valid till PI7.0.....from PI7.1 you need to make use of AbstractTransformation.....the above code makes use of StreamTransformation.

Try testing the code in PI7.0 and it may work there.

I am not a JAVA expert....so corrections are welcomed for my reply