cancel
Showing results for 
Search instead for 
Did you mean: 

Enumeration mapping in message mapping

Former Member
0 Kudos

I have a source structure ABC that contains two fields. This structure needs to be transformed into two records that will store the field name of the source as well as the value. It actually creates name/value pair in the target interface. I wonder if message mapping could possibly handle that.

[Source]

ABC

---FIELD_A = 123

---FIELD_B = 456

[Target]

DEF[0]

---NAME = "FIELD_A"

---VALUE = "123"

DEF[1]

---NAME = "FIELD_B"

---VALUE = "456"

Regards

Chong Wah

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Ananth,

I have decided to use XSL mapping instead. Anyway, thank you very much for your help.

Regards

Chong Wah

Former Member
0 Kudos

Hi Chong Wah,

You can use the following java code to solve your problem using java mapping.


/*
 * Created on Sep 14, 2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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 com.sap.aii.mapping.api.StreamTransformation;

/**
 * @author AnanthBabu Chinnaraj
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class JavaMapping implements StreamTransformation {

	private Map map;
	private Document document;
	DOMSource domS = null;
	Document docOut = null;
	HashMap[] xmlData = null;

	/**
	 * method setParamters is required, but we do not anything with it
	 */
	public void setParameter(Map param) {
		map = param;
	}

	/**
	 * method execute is called by the XI mapping program
	 */
	public void execute(InputStream in, OutputStream out) {

		HashMap[] xmlData = null;

		xmlData = parseInputXML(in);
		createOutputXML(xmlData[0], out);

	}

	public static void main(String args[]) throws Exception {
		try {
			JavaMapping mapObj = new JavaMapping();
			FileInputStream in = new FileInputStream("D:/zAnanth/SDN/Src.xml");
			FileOutputStream out =
				new FileOutputStream("D:/zAnanth/SDN/Trgt.xml");
			mapObj.execute(in, out);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * method to process input xml
	 * return array of HashMap for every 'ABC' tag
	 **/

	public HashMap[] parseInputXML(InputStream in) {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			String tagName = null;
			String tagValue = null;

			// create DOM structure from input XML
			DocumentBuilder builder = factory.newDocumentBuilder();
			document = builder.parse(in);

			// look for the tag 'ABC'
			NodeList list = document.getElementsByTagName("ABC");
			//Initialize array size
			xmlData = new HashMap[list.getLength()];

			for (int i = 0; i < list.getLength(); i++) {
				Node node = list.item(i);
				//Initialize hashmap
				xmlData<i> = new HashMap();

				//Process Child nodes
				NodeList childList = node.getChildNodes();

				for (int j = 0; j < childList.getLength(); j++) {

					Node childNode = childList.item(j);

					if (childNode != null) {

						if (childNode.getNodeType() == Node.ELEMENT_NODE) {
							//Store tag name
							tagName = childNode.getNodeName();
							//System.out.println("Name  *:"+childNode.getNodeName());

							//Store tag value
							tagValue = processValueNode(childNode);

							//Store as name value pair
							xmlData<i>.put(tagName.toUpperCase(), tagValue);
						}
					}
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return xmlData;
	}

	private String processValueNode(Node childNode) {
		String tagValue = null;
		NodeList valueNodesList = childNode.getChildNodes();

		Node valueNode = valueNodesList.item(0);

		if (valueNode != null) {

			if (valueNode.getNodeType() == Node.TEXT_NODE) {
				tagValue = valueNode.getNodeValue();
				//System.out.println("Value #:"+valueNode.getNodeValue());
			}

		}

		return tagValue;
	}

	/**
	 * Method to create xml document from input hashmaps
	 * return XML doc in InputStream
	 **/

	public void createOutputXML(HashMap xmlData, OutputStream out) {

		try {
			DocumentBuilderFactory factory =
				DocumentBuilderFactory.newInstance();
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transform = tf.newTransformer();
			DocumentBuilder builder = factory.newDocumentBuilder();

			//Create the output DOM
			docOut = builder.newDocument();

			//Create Top most Element
			Element topRoot = docOut.createElementNS("http://XYZ", "ns:MT_ABC");
			docOut.appendChild(topRoot);

			Element defNode = null;

			Set set = xmlData.keySet();

			String[] tagNames = new String[set.size()];
			set.toArray(tagNames);

			for (int i = 0; i < tagNames.length; i++) {

				defNode = createElement("DEF", topRoot);
				createElement("Name", defNode, tagNames<i>);
				createElement(
					"Value",
					defNode,
					(String) xmlData.get(tagNames<i>));
			}

			//Process XML
			domS = new DOMSource(docOut);
			transform.transform((domS), new StreamResult(out));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//Create an Element and add it into Parent
	private Element createElement(String elementName, Element parent) {

		Element ele = docOut.createElement(elementName);
		parent.appendChild(ele);

		return ele;

	}

	//Create an Element and Text node and add it into Parent
	private Element createElement(
		String elementName,
		Element parent,
		String value) {

		Element ele = docOut.createElement(elementName);
		ele.appendChild(docOut.createTextNode(value));
		parent.appendChild(ele);

		return ele;
	}

	//Get Values from Map, if value is null pass empty string
	private String getValue(HashMap map, String tagName) {
		String value = "";

		try {
			value = (String) map.get(tagName);
			if (value == null) {
				value = "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}

}

Regards,

Ananth

Former Member
0 Kudos

Hi Chong Wah,

This can be done using Java mapping. Use Java xml parsers to extract tag names and values and then build the target structure using the name/value pair.

Regards,

Ananth