SAP PI PO API for Java mapping programs JAVA 映射示例

这个示例展示了如何使用Java的DOM API来实现XML的读取和写入。它创建了一个名为`DomCopy`的类,实现了`StreamTransformation`接口,从输入流中读取XML文档,然后将其写入输出流。在处理过程中,它使用了`DocumentBuilder`来解析XML,`Transformer`来转换和写入XML。这个简单的例子突出了XML处理的基本步骤。
摘要由CSDN通过智能技术生成

用于 Java 映射程序的 API。该 API 与映射到输出流的输入流一起使用。根据用例,应用程序开发人员可以选择其他 API 来执行实际映射(例如 SAXParser 或 DocumentBuilder 来解析 InputStream)。此外,映射 API 还提供了用于映射 XI 消息的特殊标头的类,该标头允许适配器传输适配器特定的信息(类 DynamicConfiguration 和 DynamicConfigurationKey)。
尽管以下示例仅将输入负载复制到输出负载,但它实质上显示了如何使用映射 API。有关详细信息,请参阅相应接口和类中的详细信息。

SAP PI PO API for Java mapping programs JAVA 映射示例

package exampleMapping;

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.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.sap.aii.mapping.api.MappingTrace;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;

/**
 * Copy XML with a DOM.
 */
public class DomCopy implements StreamTransformation {

  private MappingTrace trace;

  /**
   * Constructor for DomCopy.
   */
  public DomCopy() {
    super();
  }

  /**
   * @see com.sap.aii.mapping.api.StreamTransformation#setParameter(Map)
   */
  public void setParameter(Map param) {
    trace = (MappingTrace) param.get(StreamTransformationConstants.MAPPING_TRACE);
  }

  /**
   * @see com.sap.aii.mapping.api.StreamTransformation#execute(InputStream,
   *      OutputStream)
   */
  public void execute(InputStream in, OutputStream out) throws StreamTransformationException {
    try {
      // read XML
      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
      Document xml = documentBuilder.parse(in);
            
      // write XML
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer = transformerFactory.newTransformer();
      Result result = new StreamResult(out);
      Source domSource = new DOMSource(xml);
      transformer.transform(domSource, result);
    } catch (ParserConfigurationException e) {
      trace.addWarning(e.getMessage());
      throw new StreamTransformationException("Can not create DocumentBuilder.", e);
    } catch (SAXException e) {
      trace.addWarning(e.getMessage());
      throw new StreamTransformationException("Can not read XML.", e);
    } catch (IOException e) {
      trace.addWarning(e.getMessage());
      throw new StreamTransformationException("Can not read XML.", e);
    } catch (TransformerConfigurationException e) {
      trace.addWarning(e.getMessage());
      throw new StreamTransformationException("Can not create Transformer.", e);
    } catch (TransformerException e) {
      trace.addWarning(e.getMessage());
      throw new StreamTransformationException("Can not write XML.", e);
    }
  }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SAP爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值