1、獲取WSDL定義和endpoit地址java
WSDL定義地址由開發者提供,爲http://10.10.xx.xxx/webservice/Pangus.SCC.OracleEBS.WebService.dll/wsdl/IWebServicenode
經過瀏覽器訪問WSDL地址,能夠看到endpoint地址的說明:web
<service name="IWebServiceservice">spring
<port name="IWebServicePort" binding="tns:IWebServicebinding">瀏覽器
<soap:address location="http://10.10.xx.xxx/webservice/Pangus.SCC.OracleEBS.WebService.dll/soap/IWebService"/>dom
</port>eclipse
</service>ide
2、eclipse生成Java WebService Client代理ui
Eclipse->New->Other->Web Service Client->輸入wsdl地址->Finish。this
生成的類代碼爲(不包含IReelIdService.java和ReelIdServiceImpl.java類):
3、調用WebService提供的方法
本例調用的是getIMSData方法,經過代理類IWebServicProxy.java調用,返回的數據爲XML,須要解析
/**
* 調用盤古WMS系統的WebService獲取REELID信息
* 返回格式是字符串,須要本身解析
* @param reelId
* @return String
*/
private String getReelIdInfoFromPangusWebService(String reelId) {
if (null == reelId || reelId.equals("")) {
return "";
}
IWebServiceProxy wsProxy = new IWebServiceProxy();
wsProxy.setEndpoint("http://10.10.xx.xxx/webservice/Pangus.SCC.OracleEBS.WebService.dll/soap/IWebService");
StringHolder resultDTOXml = new StringHolder();
StringHolder resultMsg = new StringHolder();
try {
Boolean success = wsProxy.getIMSData("REELID", reelId, resultDTOXml, resultMsg);
if (!success) {
throw new MESException("ERR-0002");
}
} catch (RemoteException e) {
e.printStackTrace();
throw new MESException("ERR-0002");
}
return resultDTOXml.value;
}
4、解析XML
經過XPath解析返回的XML數據,幷包裝成VO。XPath有點相似於JQuery,它對XML解析,而JQuery是對DOM進行解析,XPath的具體知識可參考:http://w3school.com.cn/xpath/
/**
* 解析盤古WMS WebService返回的REELID字符串
* 格式:<DataSet><row><REELID>T905555273842-130706-0008</REELID><DATECODE/><QTY>5000.000</QTY><MTRL_ID>307674</MTRL_ID><STATUS>0</STATUS><CUST_REELID/></row></DataSet>
* @param reelIdInfo REELID字符串
* @return ReelIdVO
*/
private ReelIdVO parseXmlToVO(String reelIdInfo) {
System.out.println(reelIdInfo);
LOG.debug("reelIdInfo = " + reelIdInfo);
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
// 增長XML定義
//String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + reelIdInfo;
StringReader sr = new StringReader(reelIdInfo);
InputSource is = new InputSource(sr);
Document doc = builder.parse(is);
//Document doc = builder.parse(new File("C:/test.xml"));
// 經過XPath查找XML節點數據
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
// 根下的DataSet下的第一個row下的REELID元素文本
XPathExpression expr = xPath.compile("/DataSet/row[1]//*");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
if (null == nodes) {
return null;
}
ReelIdVO reelIdVO = new ReelIdVO();
// 庫存組織
reelIdVO.setOrganizationId(Long.valueOf(getNodeValueByNodeName(nodes, "organizationId")));
// REELID
reelIdVO.setReelId(getNodeValueByNodeName(nodes, "REELID"));
// 剩餘數量
reelIdVO.setRemainQty(Double.valueOf(getNodeValueByNodeName(nodes, "QTY")));
// 原始數量
reelIdVO.setQty(Double.valueOf(reelIdVO.getRemainQty()) + Double.valueOf(getNodeValueByNodeName(nodes, "usedQTY")));
reelIdVO.setStatus(getNodeValueByNodeName(nodes, "STATUS"));
reelIdVO.setManufactory(getNodeValueByNodeName(nodes, "manufactory"));
reelIdVO.setDateCode(getNodeValueByNodeName(nodes, "DATECODE"));
reelIdVO.setLotCode(getNodeValueByNodeName(nodes, "LOTCODE"));
reelIdVO.setInventoryItemId(Long.valueOf(getNodeValueByNodeName(nodes, "MTRL_ID")));
reelIdVO.setCustReelId(getNodeValueByNodeName(nodes, "CUST_REELID"));
return reelIdVO;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return null;
}
代碼以下:
package cn.com.scc.wms.biz.reelid; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.rmi.RemoteException; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.rpc.holders.StringHolder; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import cn.com.scc.common.sys.MESException; import cn.com.scc.wms.vo.ReelIdVO; /** * REELID業務方法 * @author XUHL * @deprecated 從盤古WebService獲取數據 * */ //@Service(value = "reelIdServiceImpl") public class ReelIdServiceImpl implements IReelIdService { private final Logger LOG = LoggerFactory.getLogger(getClass()); @Override public ReelIdVO getByReelId(String reelId) { String reelIdString = this.getReelIdInfoFromPangusWebService(reelId); return this.parseXmlToVO(reelIdString); } @Override public List<ReelIdVO> getAllByInventoryItemNumber(String inventoryItemNumber) { // TODO Auto-generated method stub return null; } @Override public List<ReelIdVO> getAllByCustReelId(String custReelId) { // TODO Auto-generated method stub return null; } /** * 調用盤古WMS系統的WebService獲取REELID信息 * 返回格式是字符串,須要本身解析 * @param reelId * @return String */ private String getReelIdInfoFromPangusWebService(String reelId) { if (null == reelId || reelId.equals("")) { return ""; } IWebServiceProxy wsProxy = new IWebServiceProxy(); wsProxy.setEndpoint("http://10.10.1.109/webservice/Pangus.SCC.OracleEBS.WebService.dll/soap/IWebService"); StringHolder resultDTOXml = new StringHolder(); StringHolder resultMsg = new StringHolder(); try { Boolean success = wsProxy.getIMSData("REELID", reelId, resultDTOXml, resultMsg); if (!success) { throw new MESException("ERR-0002"); } } catch (RemoteException e) { e.printStackTrace(); throw new MESException("ERR-0002"); } return resultDTOXml.value; } /** * 解析盤古WMS WebService返回的REELID字符串 * 格式:<DataSet><row><REELID>T905555273842-130706-0008</REELID><DATECODE/><QTY>5000.000</QTY><MTRL_ID>307674</MTRL_ID><STATUS>0</STATUS><CUST_REELID/></row></DataSet> * @param reelIdInfo REELID字符串 * @return ReelIdVO */ private ReelIdVO parseXmlToVO(String reelIdInfo) { System.out.println(reelIdInfo); LOG.debug("reelIdInfo = " + reelIdInfo); try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); // 增長XML定義 //String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + reelIdInfo; StringReader sr = new StringReader(reelIdInfo); InputSource is = new InputSource(sr); Document doc = builder.parse(is); //Document doc = builder.parse(new File("C:/test.xml")); // 經過XPath查找XML節點數據 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); // 根下的DataSet下的第一個row下的REELID元素文本 XPathExpression expr = xPath.compile("/DataSet/row[1]//*"); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); if (null == nodes) { return null; } ReelIdVO reelIdVO = new ReelIdVO(); // 庫存組織 reelIdVO.setOrganizationId(Long.valueOf(getNodeValueByNodeName(nodes, "organizationId"))); // REELID reelIdVO.setReelId(getNodeValueByNodeName(nodes, "REELID")); // 剩餘數量 reelIdVO.setRemainQty(Double.valueOf(getNodeValueByNodeName(nodes, "QTY"))); // 原始數量 reelIdVO.setQty(Double.valueOf(reelIdVO.getRemainQty()) + Double.valueOf(getNodeValueByNodeName(nodes, "usedQTY"))); reelIdVO.setStatus(getNodeValueByNodeName(nodes, "STATUS")); reelIdVO.setManufactory(getNodeValueByNodeName(nodes, "manufactory")); reelIdVO.setDateCode(getNodeValueByNodeName(nodes, "DATECODE")); reelIdVO.setLotCode(getNodeValueByNodeName(nodes, "LOTCODE")); reelIdVO.setInventoryItemId(Long.valueOf(getNodeValueByNodeName(nodes, "MTRL_ID"))); reelIdVO.setCustReelId(getNodeValueByNodeName(nodes, "CUST_REELID")); return reelIdVO; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XPathExpressionException e) { e.printStackTrace(); } return null; } private String getNodeValueByNodeName(NodeList nodes, String nodeName) { for (int i = 0; i < nodes.getLength(); i++) { if (nodes.item(i).getNodeName().equals(nodeName)) { return nodes.item(i).getTextContent(); } } return ""; } }