本文章分享如何创建 Java 映射,使用的 PI 版本为 7.4(Java Only)。当他们需要创建复杂的映射时,这将对他们有所帮助。对于此示例,使用 DOM 对象,我添加了示例代码。
1. 下载 NWDS (Netweaver Developer Studio) https://nwds.sap.com/swdc/downloads/updates/netweaver/nwds/nw/730/
2. 下载 JDK (Java Development kit) Java Downloads | Oracle
3. 安装 Java 和 NWDS
4. 开启新世界百货
5. 创建新的 java 项目
6. 添加 XPI 库
7. 创建新类 (右键单击 src)
8. 添加代码 (以 Dom 对象为例)
XML 出站和入站(示例):
<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_test xmlns:ns0="urn:elrosado.com:switchTransaccional:listaNegra">
<row>
<nombre>Juan</nombre>
<apellido>Padilla</apellido>
<edad>32</edad>
</row>
</ns0:MT_test>
代码:
package com.test.mapping;
import java.io.InputStream;
import java.io.OutputStream;
import com.sap.aii.mapping.api.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test_JavaMapping extends AbstractTransformation {
@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
String RESULT = new String();
String inicio = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ns0:MT_test xmlns:ns0=\"urn:elrosado.com:switchTransaccional:listaNegra\">";
String fin = "</ns0:MT_test>";
String nombre = "";
String apellido = "";
String edad = "";
try {
InputStream inputstream = transformationInput.getInputPayload().getInputStream();
OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputstream);
NodeList children = doc.getElementsByTagName("row");
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) node;
nombre = eElement.getElementsByTagName("nombre").item(0).getTextContent();
apellido = eElement.getElementsByTagName("apellido").item(0).getTextContent();
edad = eElement.getElementsByTagName("edad").item(0).getTextContent();
RESULT = RESULT + "<row>" + "<nombre>" + nombre + " " + apellido + "</nombre>" +
"<apellido>" + nombre + " " + apellido + "</apellido>" + "<edad>" +edad+"</edad>"+"</row>";
}
}
RESULT = inicio + RESULT + fin;
outputstream.write(RESULT.getBytes());
} catch (Exception exception) {
getTrace().addDebugMessage(exception.getMessage()+ RESULT);
throw new StreamTransformationException(exception.toString()+ RESULT);
}
}
}
9. 导出 jar
10. 将 jar 上传到 PI (Enterprise Service Builder)
11. 保存并激活
12. 在操作映射中使用