cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with Java DOM Mapping

Former Member
0 Kudos

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" ?>

- <xsd:schema targetNamespace="urn:agrp:xi:geissseb" xmlns:xsd="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>

<xsd:appinfo source="http://sap.com/xi/TextID">a7a28830bebf11dc81aa001a4b0af224</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="f01" 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>

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Here you go.

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.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.apache.crimson.tree.TextNode;

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

public class DomTest {

Document documentOut = null;

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

{

try

{

FileInputStream fin = new FileInputStream("test.xml");

FileOutputStream fout = new FileOutputStream("test_out.xml");

DomTest mapping = new DomTest();

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 NodeList10;

//Element[] field=new Element10;

documentIn = builder.parse(in);

documentOut = builder.newDocument();

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

rootNode.setAttribute("xmlns:ns0", "urn:agrp:xi:geissseb");

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 = 0 ; j < list_Set.getLength() ;j++){

Element setNode = (Element)list_Set.item(j);

NodeList children = setNode.getChildNodes();

for(int index = 0; index < children.getLength(); index++)

{

Node child = children.item(index);

if (child instanceof Element) {

Element element = (Element) child;

if(element.getNodeName().startsWith("set_ELEMENT"))

createItemFeildNode(tableNode,element);

}

}

}

//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));

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

} catch (SAXException e2) {

e2.printStackTrace();

} catch (IOException e2) {

e2.printStackTrace();

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

}

private void createItemFeildNode(Element root, Element element) {

String inName = element.getNodeName();

int temp = inName.lastIndexOf('_');

if (temp == -1)

return;

String index = inName.substring(temp+1);

String nodeName = "Item_FIELD_" + index;

String nodeVal = element.getFirstChild().getNodeValue();

Element newElement = documentOut.createElement(nodeName);

Text txtNode = documentOut.createTextNode(nodeVal);

newElement.appendChild(txtNode);

root.appendChild(newElement);

}

}

Input

<?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>

Output

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

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

<TABLE>

<Item_FIELD_01>E1</Item_FIELD_01>

<Item_FIELD_02>E2</Item_FIELD_02>

<Item_FIELD_03>E3</Item_FIELD_03>

<Item_FIELD_04>E4</Item_FIELD_04>

<Item_FIELD_05>E5</Item_FIELD_05>

<Item_FIELD_06>E6</Item_FIELD_06>

<Item_FIELD_07>E7</Item_FIELD_07>

<Item_FIELD_08>E8</Item_FIELD_08>

<Item_FIELD_09>E9</Item_FIELD_09>

<Item_FIELD_10>E10</Item_FIELD_10>

</TABLE>

</ns0:mt_MappingIn>

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Kanwal,

problem solved. It was just me beeing wrong while executing the test in the repository...

Thank you very much for your help,

Sebastian

Edited by: Sebastian Geissler on Jan 14, 2008 12:21 PM

Former Member
0 Kudos

Hi Kenwal,

Thankk you so far. It does workperfectly for the Inpit you provides. But if I take the input I provided the code does not seem to find the tag <set>

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

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

resulst in 0.

And therefore the output is still:<?xml version="1.0" encoding="UTF-8" standalone="no"?><ns0:mt_MappingIn xmlns:ns0="urn:agrp:xi:geissseb"><TABLE/></ns0:mt_MappingIn> so the content is missing....

Cheers,

Sebastian

Former Member
0 Kudos

The input provided by you in this post is a xsd and not xml. Please give me the xml file you want to test with so that I can find out the error if any.

The easiest way to get the xml would be to go the Interface Mapping Test Tab, and fill out the required values and then copy the src and paste it here. I could Debug and see if I get a problem.

Thanks and Best Regards,

Kanwal