标签:java webservice
一、获取WSDL定义和endpoit地址
WSDL定义地址由开发者提供,为http://10.10.xx.xxx/webservice/Pangus.SCC.OracleEBS.WebService.dll/wsdl/IWebService
通过浏览器访问WSDL地址,可以看到endpoint地址的说明:
<service name="IWebServiceservice">
<port name="IWebServicePort" binding="tns:IWebServicebinding">
<soap:address location="http://10.10.xx.xxx/webservice/Pangus.SCC.OracleEBS.WebService.dll/soap/IWebService"/>
二、eclipse生成Java WebService Client代理
Eclipse->New->Other->Web Service Client->输入wsdl地址->Finish。
生成的类代码为(不包含IReelIdService.java和ReelIdServiceImpl.java类):
三、调用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;
}
四、解析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 ""; } }
JAVA调用返回XML格式数据的WebService,并通过XPath解析XML的应用
标签:java webservice
原文地址:http://9966064.blog.51cto.com/9956064/1772194