---
title: "XML合并code"
author: "vikingwei"
date: "2018-08-13T15:17:25Z"
tags: []
categories: ["编程语言"]
slug: "xml-code"
draft: false
lastmod: "2018-08-20T16:38:22Z"
id: 2428612
url: "https://vikingwei.iteye.com/blog/2428612"
---

//--不管webservice要求请求是JSON还是XML都可以用XML发送请求。  
//http://871421448.iteye.com/blog/1546955  
org.dom4j.Document document = org.dom4j.DocumentHelper.createDocument();  
org.dom4j.Element root = document.addElement("SAP\_ESB");

//增加一个父节点只需在后面加一个document.addElement节点即可.

//org.dom4j.Element root = document.addElement("SAP\_ESB").addElement("TEST");

//context.json\_purdm为countext变量,类型为Object;订购单主档xml资料.  
//资料流为:tOracleInput控件--&gt;tXMLMap--&gt;tJavaRow,就是db中取出数据并打包成xml,最后存入context.json\_purdm变量中.  
// ADD NODE PURM  
try{  
    routines.system.Document jsonPurm = (routines.system.Document)context.json\_purdm;  
    org.dom4j.Document docm = jsonPurm.getDocument();  
    org.dom4j.Element rootPurm = docm.getRootElement();  
    root.add(rootPurm);  
}  
catch(Exception ex){  
    root.addElement("ETL\_PURM");  
}

// ADD NODE PURD  
try{  
    routines.system.Document jsonPurd = (routines.system.Document)context.json\_purdd;  
    org.dom4j.Document docd = jsonPurd.getDocument();  
    org.dom4j.Element rootPurd = docd.getRootElement();  
    root.add(rootPurd);  
}  
catch(Exception ex){  
    root.addElement("ETL\_PURD");  
}

// ADD NODE PURD\_SIZE  
try{  
    routines.system.Document jsonSize = (routines.system.Document)context.json\_purdsize;  
    org.dom4j.Document docs = jsonSize.getDocument();  
    org.dom4j.Element rootSize = docs.getRootElement();  
    root.add(rootSize);  
}  
catch(Exception ex){  
    root.addElement("ETL\_PURDSIZE");  
}

routines.system.Document docReturn = new routines.system.Document();  
docReturn.setDocument(document);

//XML Format to Json  
context.json\_body = docReturn;

System.out.println("json\_body:" + context.json\_body);

//最后请求时,要用tFixedFlowInput做为tRESTClient请求的输入,但需要强制转换为Document：(Document)context.json\_body;context.json\_body是Object类型的,所以要转换。

![](http://dl2.iteye.com/upload/attachment/0130/4561/f1eef199-105c-3623-9e44-9e5ef03471bb.jpg)