外部系统xml数据发送至NC

来源:互联网 发布:mirrorlink认证软件 编辑:程序博客网 时间:2024/06/02 06:14
在做外部系统xml数据发送至NC的测试中,按照用友给的示例代码,出现了一些错误。


首先是找不到XMLUtil这个类。后来,用解压软甲打开NC系统安装包(170几兆)中的setup.jar文件,在NCCACHE/server3_D--ufsoft-nchome_80/CODE/lib中找到basic.jar,XMLUtil这个类就在里面。将basic.jar解压出来,在eclipse中,右击项目>build path>add extra archives>选择basic.jar。回到eclipse 代码编辑界面中,import nc.vo.jcom.xml.XMLUtil;


但是,回执信息却提示 从输入流转换Document出错。后来,使用jdom(版本2.0.6,跟上面的basic.jar一样,需要导入)来处理终于可以了:
</pre><pre name="code" class="java">import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import org.jdom2.input.DOMBuilder;import org.jdom2.input.SAXBuilder;import org.jdom2.output.XMLOutputter;import org.w3c.dom.Document;import nc.vo.jcom.xml.XMLUtil;public class XDTest {public static void main(String[] args) {String url = ".........";URL realURL;try {realURL = new URL(url);HttpURLConnection connection = (HttpURLConnection) realURL.openConnection();connection.setDoOutput(true);connection.setRequestProperty("content-type", "text/xml");connection.setRequestMethod("POST");// 将Document对象写入连接的输出流中File file = new File("C:/Users/wuqingyi/Desktop/凭证.xml");InputStream input = new FileInputStream(file);Document doc = XMLUtil.getDocumentBuilder().parse(input);// // 构造器SAXBuilder saxBuilder = new SAXBuilder();// // 设定格式XMLOutputter out = new XMLOutputter();///////////////////////////////////////注意这里,doc需要转换成JDOM的Document类/////////////////////////////////out.output(new DOMBuilder().build(doc), connection.getOutputStream());// 从连接的输入流中取得回执信息InputStream inputStream = connection.getInputStream();org.jdom2.Document jDoc = saxBuilder.build(inputStream);out.output(jDoc, new OutputStreamWriter(new FileOutputStream(new File("C:/Users/wuqingyi/Desktop/ret.xml")),"UTF-8"));} catch (Exception e1) {e1.printStackTrace();}}}


主要问题在示例代码中的("content-type", "text/xml");而不是("contect-type", "text/xml");其次是XMLUtil.printDOMTree(writer, doc, 1);这个printDOMTree根本没用。注意:doc是org.w3c.dom.Document的Document类,而上面代码中的new DOMBuilder().build(doc)返回的是JDOM中的Document类
0 0