Skip to Content
0
Former Member
Jan 09, 2008 at 03:49 PM

Problems with Java DOM Mapping

129 Views

Hi Experts,

as part of my diploma-thesis I have to write a java DOM-Mapping, which mapps the following incoming message:

<mt_MappingOUT>

<set>

<set_Element_01>...</set_Element_01>

<set_Element_02>... </set_Element_02>

<set_Element_03>... </set_Element_02>

.

.

<set_Element_10> </set_Element_10>

</set>

</mt_MappingOUT>

to the following outgoinig message:

<mt_MappingIN>

<TABLE>

<item>

<item_FIELD_01> </item_FIELD_01>

.

.

.

<item_FIELD_10> <item_FIELD_10>

<i/tem>

</TABLE>

</mt_MappingIN>

I am not a very experienced Java-Developer. You can see my code down there, which I tried to implement according to some bloggs; unfortunately the result is not what I want to have

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.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.w3c.dom.Text;

import org.xml.sax.SAXException;

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

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

public class DOMMAPPING implements StreamTransformation{

public static void main(String[] args) throws Exception

{

try

{

FileInputStream fin =

new FileInputStream("C:/mt_MappingOut.xml");

FileOutputStream fout =

new FileOutputStream("C:/target.xml");

DOMMAPPING mapping = new DOMMAPPING();

mapping.execute(fin, fout);

}

catch (Exception e) {

e.printStackTrace();

}

}

public void setParameter (Map param) {}

public void execute (InputStream in, OutputStream out)

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

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

Document documentIn = null;

DocumentBuilder builder = null;

try {

builder = factory.newDocumentBuilder();

} catch (ParserConfigurationException e1) {

e1.printStackTrace();

}

try {

Element itemNode = null;

NodeList[] list_f=new NodeList[10];

Element[] field=new Element[10];

documentIn = builder.parse(in);

Document documentOut = builder.newDocument();

Element rootNode = documentOut.createElementNS("urn:agrp:xi:geissseb","ns0:mt_MappingIn");

documentOut.appendChild(rootNode);

Element tableNode = documentOut.createElement("TABLE");

rootNode.appendChild(tableNode);

NodeList list_Set=documentIn.getElementsByTagName("set");

System.out.println(list_Set.getLength());

for(int j=1;j<10;j++){

list_f[j-1]=documentIn.getElementsByTagName("set_ELEMENT_01"+j);

}

list_f[9]=documentIn.getElementsByTagName("set_ELEMENT_10");

//NodeList list_f01=documentIn.getElementsByTagName("f01");

for (int i=0;i<list_Set.getLength();i++)

{

itemNode=documentOut.createElement("item");

tableNode.appendChild(itemNode);

for(int k=0; k<10;k++){

Node f=list_f[k].item(i);

f=f.getFirstChild();

String str_f=f.getNodeValue();

Text text_f=documentOut.createTextNode(str_f);

field[k]=documentOut.createElement("item_FIELD_"(k1));

field[k].appendChild(text_f);

itemNode.appendChild(field[k]);

}

}

TransformerFactory tf = TransformerFactory.newInstance();

Transformer transform = tf.newTransformer();

transform.transform(new DOMSource(documentOut), new StreamResult(out));

} catch (SAXException e2) {

e2.printStackTrace();

} catch (IOException e2) {

e2.printStackTrace();

}catch (Throwable t) { throw new StreamTransformationException("error", t); }

}

}

Unfortunately there seems to be at least one error in there because the result I get is just the following:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

- <ns0:mt_MappingIn xmlns:ns0="urn:agrp:xi:geissseb">

<TABLE />

</ns0:mt_MappingIn>

Below you will find my source message:

<?xml version="1.0" encoding="UTF-8" ?>

- http://www.w3.org/2001/XMLSchema" xmlns="urn:agrp:xi:geissseb">

<xsd:element name="mt_MappingOut" type="dt_MappingOut" />

- <xsd:complexType name="dt_MappingOut">

- <xsd:annotation>

a7a28830bebf11dc81aa001a4b0af224http:// a7a28830bebf11dc81aa001a4b0af224" target="_blank">sap.com/xi/TextID">a7a28830bebf11dc81aa001a4b0af224>

</xsd:annotation>

- <xsd:sequence>

- <xsd:element name="Set">

- <xsd:annotation>

0b366510aeda11dcb3bb00174205b856http:// 0b366510aeda11dcb3bb00174205b856" target="_blank">sap.com/xi/TextID">0b366510aeda11dcb3bb00174205b856>

</xsd:annotation>

- <xsd:complexType>

- <xsd:sequence>

- <xsd:element name="f01" type="xsd:string">

- <xsd:annotation>

0b366511aeda11dcaf2600174205b856http:// 0b366511aeda11dcaf2600174205b856" target="_blank">sap.com/xi/TextID">0b366511aeda11dcaf2600174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_02" type="xsd:string">

- <xsd:annotation>

0b366512aeda11dc84d400174205b856http:// 0b366512aeda11dc84d400174205b856" target="_blank">sap.com/xi/TextID">0b366512aeda11dc84d400174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_03" type="xsd:string">

- <xsd:annotation>

0b366513aeda11dcbcab00174205b856http:// 0b366513aeda11dcbcab00174205b856" target="_blank">sap.com/xi/TextID">0b366513aeda11dcbcab00174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_04" type="xsd:string">

- <xsd:annotation>

0b366514aeda11dc96a300174205b856http:// 0b366514aeda11dc96a300174205b856" target="_blank">sap.com/xi/TextID">0b366514aeda11dc96a300174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_05" type="xsd:string">

- <xsd:annotation>

0b366515aeda11dca77700174205b856http:// 0b366515aeda11dca77700174205b856" target="_blank">sap.com/xi/TextID">0b366515aeda11dca77700174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_06" type="xsd:string">

- <xsd:annotation>

0b366516aeda11dc8f7d00174205b856http:// 0b366516aeda11dc8f7d00174205b856" target="_blank">sap.com/xi/TextID">0b366516aeda11dc8f7d00174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_07" type="xsd:string">

- <xsd:annotation>

0b366517aeda11dcc24b00174205b856http:// 0b366517aeda11dcc24b00174205b856" target="_blank">sap.com/xi/TextID">0b366517aeda11dcc24b00174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_08" type="xsd:string">

- <xsd:annotation>

0b366518aeda11dc92cd00174205b856http:// 0b366518aeda11dc92cd00174205b856" target="_blank">sap.com/xi/TextID">0b366518aeda11dc92cd00174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_09" type="xsd:string">

- <xsd:annotation>

0b366519aeda11dcc9b100174205b856http:// 0b366519aeda11dcc9b100174205b856" target="_blank">sap.com/xi/TextID">0b366519aeda11dcc9b100174205b856>

</xsd:annotation>

</xsd:element>

- <xsd:element name="set_ELEMENT_10" type="xsd:string">

- <xsd:annotation>

0b36651aaeda11dcc5a700174205b856http:// 0b36651aaeda11dcc5a700174205b856" target="_blank">sap.com/xi/TextID">0b36651aaeda11dcc5a700174205b856>

</xsd:annotation>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:schema>

When I had problems withe Java SAX Mapping, I got great hints in the forum. So I hope you can help me out again...

Cheers Sebastian