在作外部系統xml數據發送至NC的測試中,按照用友給的示例代碼,出現了一些錯誤。java
首先是找不到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;dom
可是,回執信息卻提示 從輸入流轉換Document出錯。後來,使用jdom(版本2.0.6,跟上面的basic.jar同樣,須要導入)來處理終於能夠了:eclipse
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類測試