on 01-07-2008 3:53 PM
Hi Experts,
as part of my diploma-thesis I have to write a java SAX-Mapping, which mapps the following incoming message:
mt_MappingOUT
set
set_Element_01
set_Element_02
set_Element_03
.
.
set_Element_10
to the following outgoinig message:
mt_MappingIN
TABLE
item
item_FIELD_01
.
.
.
item_FIELD_10
I develped the following code, which unfortunately results in "XML is not well defined" while testing. Unfortunately I cannot find the problem. So I hope someone of you can have a look an will have an idea.
/*
Created on 20.12.2007
*/
/**
@author Sebastian Geissler
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.sap.aii.mapping.api.*;
import java.io.*;
import java.util.Map; import javax.xml.parsers.*;
import org.xml.sax.; import org.xml.sax.helpers.;
public class SETtoITEMjavaSAX extends DefaultHandler implements StreamTransformation
{
private Map map;
private OutputStream out;
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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
write("<ns0:mt_MappingIn xmlns:ns0=\"urn:agrp:xi:geissseb\"><TABLE>");
}
public void endDocument () throws SAXException
{
write("</TABLE></ns0:mt_MappingIn>");
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("set"))
write("<item>");
if(eName.substring(0,6).equals("set_E"))
write("<item_FIELD"eName.substring(10,13)">");
}
public void endElement (String namespaceURI, String sName, String qName) throws SAXException
{
String eName = sName;
if ("".equals(eName))
eName = qName;
if(eName.equals("set"))
write("</item>");
if(eName.substring(0,6).equals("set_E"))
write("</item_FIELD"eName.substring(10,13)">");
}
public void characters (char buf[], int offset, int len)
throws SAXException {
String s = new String(buf, offset, len);
write (s);
}
}
Thank you,
Sebastian
Hi Kanwal,
thank you so much!!1 I did some minor changes and now it works.
I still get the error-message XML is not well defined, when testing in the repository, but the resulting message lokks exactliy like the messages created via graphical mapping.
I don't understand the error-message, but for my purposes I do not need to care about this...
Cheers,
Sebastian
P.S: I awarded the points for your answer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The Problem was that XML is case sensitive and the Tag name in start element Item_FIELD was not matching the Tag Name in End Element. I have changed the code for End Element. This should solve the problem now.
Code
public void endElement (String namespaceURI, String sName, String qName) throws SAXException {
String eName = sName;
if ("".equals(eName))
eName = qName;
if(eName.equals("set"))
write("</item>");
if(eName.startsWith("set_ELEMENT"))
write("</Item_FIELD_" + eName.substring(13,14) + ">");
}
Hi Kanwal,
oh my good, how could I be that blind....
Thanks,
Sebastian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ravi, hi Raman,
thanks so far... I did some changes in my code, but I am still having the same problem... down there you will find my new code and the source message.
there is my new code:
private Map map;
private OutputStream out;
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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
write("<ns0:mt_mappingOUT xmlns:ns0=\"urn:agrp:xi:geissseb\"><TABLE>");
}
public void endDocument () throws SAXException {
write("</TABLE></ns0:mt_mappingOUT>");
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("mtset"))
write("<item>");
if(eName.substring(0,2).equals("se"))
write("<Item_FIELD_"eName.substring(13,14)">");
}
public void endElement (String namespaceURI, String sName, String qName) throws SAXException {
String eName = sName;
if ("".equals(eName))
eName = qName;
if(eName.equals("mtset"))
write("</item>");
if(eName.substring(0,2).equals("se"))
write("</item_FIELD_"eName.substring(13,14)">");
}
public void characters (char buf[], int offset, int len)
throws SAXException {
String s = new String(buf, offset, len);
write (s);
}
}
and there is the source message:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:agrp:xi:geissseb" targetNamespace="urn:agrp:xi:geissseb">
<xsd:element name="mt_MappingOut" type="dt_MappingOut" />
<xsd:complexType name="dt_MappingOut">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
4c79fbb0bde711dcae43001a4b0af224
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="set">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366510aeda11dcb3bb00174205b856
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="set_ELEMENT_01" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366511aeda11dcaf2600174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_02" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366512aeda11dc84d400174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_03" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366513aeda11dcbcab00174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_04" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366514aeda11dc96a300174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_05" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366515aeda11dca77700174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_06" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366516aeda11dc8f7d00174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_07" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366517aeda11dcc24b00174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_08" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366518aeda11dc92cd00174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_09" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b366519aeda11dcc9b100174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="set_ELEMENT_10" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">
0b36651aaeda11dcc5a700174205b856
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
I really hope you can help me there..
Thanks in advance..
Sebastian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The probelm was with the code, to be more precise the condition checks,
Please find below a quick fix of the code according to what I understand if the requirement.
It needs to be enhanced for more error handling still.
Please award points if you find this useful.
JAVA CODE
package com.sap.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXTest extends DefaultHandler implements StreamTransformation{
private Map map;
private OutputStream out;
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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
write("<ns0:mt_mappingOUT xmlns:ns0=\"urn:agrp:xi:geissseb\"><TABLE>");
}
public void endDocument () throws SAXException {
write("</TABLE></ns0:mt_mappingOUT>");
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("set"))
write("<item>");
if(eName.startsWith("set_ELEMENT"))
write("<Item_FIELD_" + eName.substring(13,14) + ">");
}
public void endElement (String namespaceURI, String sName, String qName) throws SAXException {
String eName = sName;
if ("".equals(eName))
eName = qName;
if(eName.equals("set"))
write("</item>");
if(eName.startsWith("set_ELEMENT"))
write("</item_FIELD_" + eName.substring(13,14) + ">");
}
public void characters (char buf[], int offset, int len)
throws SAXException {
String s = new String(buf, offset, len);
write (s);
}
public static void main(String[] args)throws Exception {
FileInputStream fin = new FileInputStream("test.xml");
SAXTest map = new SAXTest();
map.execute(fin, System.out);
}
}
Test Input Generated from the Input XSD
<?xml version="1.0" encoding="UTF-8"?>
<mt_MappingOut xmlns="urn:agrp:xi:geissseb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:agrp:xi:geissseb file:/D:/Eclipse/workspace/SDN/test.xsd">
<set xmlns="">
<set_ELEMENT_01>E1</set_ELEMENT_01>
<set_ELEMENT_02>E2</set_ELEMENT_02>
<set_ELEMENT_03>E3</set_ELEMENT_03>
<set_ELEMENT_04>E4</set_ELEMENT_04>
<set_ELEMENT_05>E5</set_ELEMENT_05>
<set_ELEMENT_06>E6</set_ELEMENT_06>
<set_ELEMENT_07>E7</set_ELEMENT_07>
<set_ELEMENT_08>E8</set_ELEMENT_08>
<set_ELEMENT_09>E9</set_ELEMENT_09>
<set_ELEMENT_10>E10</set_ELEMENT_10>
</set>
</mt_MappingOut>
Corresponding Output
<?xml version="1.0" encoding="UTF-8"?><ns0:mt_mappingOUT xmlns:ns0="urn:agrp:xi:geissseb"><TABLE>
<item>
<Item_FIELD_1>E1</item_FIELD_1>
<Item_FIELD_2>E2</item_FIELD_2>
<Item_FIELD_3>E3</item_FIELD_3>
<Item_FIELD_4>E4</item_FIELD_4>
<Item_FIELD_5>E5</item_FIELD_5>
<Item_FIELD_6>E6</item_FIELD_6>
<Item_FIELD_7>E7</item_FIELD_7>
<Item_FIELD_8>E8</item_FIELD_8>
<Item_FIELD_9>E9</item_FIELD_9>
<Item_FIELD_0>E10</item_FIELD_0>
</item>
</TABLE></ns0:mt_mappingOUT>
*******************************************************************************************
Edited by: Kanwaljit Singh on Jan 8, 2008 7:12 PM
HI,
well the resulting file looks like this...
<?xml version="1.0" encoding="UTF-8"?><ns0:mt_MappingIn xmlns:ns0="urn:agrp:xi:geissseb"><TABLE><item>
and this is all I get... So there are content and closing tags missing...
The problem is I do not understand why this is happening, cause I found some Java-Mappings doing the same thing and for my understanding my one is looking the same (except for different names).
Cheers,
Sebastian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
it seems totally related to the coding.
It must be message specific, to meet exactly the input message.
Also, check the substrings. For example:
>if(eName.substring(0,6).equals("set_E"))
You're checking a 6-char-long string (the result of substring) with a 5-char-long string ("set_E"). They will never be equal.
There is also
>write("<item_FIELD" + eName.substring(10,13) + ">");
Are you even sure that the input element name have at least 14 chars (the char at position 13 is the 14th char)?
Maybe you should double check your code.
Regards,
Henrique.
Sebastian,
Can you check for all tags being closed..etc..
This is one thing i do...use xml spy and validate the xml for being correctly formed..........that should help...then re-run the test, and let us know the results
Regards
Ravi Raman
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
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.