Java calls return XML-formatted data webservice, and the application of XML parsing through XPath

Source: Internet
Author: User
Tags soap xpath wsdl

First, get the WSDL definition and Endpoit address

The WSDL definition address is provided by the developer for Http://10.10.xx.xxx/webservice/Pangus.SCC.OracleEBS.WebService.dll/wsdl/IWebService

To access the WSDL address through the browser, you can see a description of the endpoint address:

<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" />

</port>

</service>


Second, Eclipse generates Java WebService Client Agent

Eclipse->new->other->web Service Client-> Enter the WSDL address->finish.

The generated class code is (not including Ireelidservice.java and Reelidserviceimpl.java classes): 650) this.width=650; "Src=" http://ekp.scc.com.cn/ekp/ RESOURCE/FCKEDITOR/EDITOR/FILEMANAGER/DOWNLOAD?FDID=1407A9E548F74F6234FE80D481E8B3DC "width=" 291 "height=" 194 "/ >

Third, call the method provided by WebService

This example calls the Getimsdata method, which is called through the proxy class Iwebservicproxy.java, and the returned data is XML, which needs to be parsed

/**
* Call Pangu WMS System WebService to get reelid information
* Return format is a string that needs to be parsed by itself
* @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;
}

Iv. Parsing xml

Parses the returned XML data through XPath and wraps it into VO. XPath is a bit like jquery, which parses XML, and jquery is parsing the DOM, and the specific knowledge of XPath can be consulted: http://w3school.com.cn/xpath/

/**
* Parse the Reelid string returned by Pangu WMS WebService
* Format: <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 string
* @return Reelidvo
*/
Private Reelidvo Parsexmltovo (String reelidinfo) {
System.out.println (Reelidinfo);
Log.debug ("Reelidinfo =" + Reelidinfo);
try {
Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Dbf.newdocumentbuilder ();
adding XML definitions
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"));
Finding XML node data through XPath
Xpathfactory factory = Xpathfactory.newinstance ();
XPath XPath = Factory.newxpath ();
Reelid element text under the first row under a dataset under the root
XPathExpression expr = xpath.compile ("/dataset/row[1]//*");
NodeList nodes = (NodeList) expr.evaluate (Doc, Xpathconstants.nodeset);
if (null = = nodes) {
return null;
}
Reelidvo Reelidvo = new Reelidvo ();
Inventory Organization
Reelidvo.setorganizationid (long.valueof (Getnodevaluebynodename (nodes, "OrganizationId"));
Reelid
Reelidvo.setreelid (Getnodevaluebynodename (nodes, "reelid"));
Remaining quantity
Reelidvo.setremainqty (double.valueof (Getnodevaluebynodename (nodes, "QTY"));
Original quantity
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;
}


The code is as follows:

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 Business Methods  *  @author  xuhl  *  @deprecated    get data from Pangu 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) &NBSP;{&NBSP;&NBSP;&NBSP;&Nbsp;    // todo auto-generated method stub         return null;    }     @Override      public list<reelidvo> getallbycustreelid (string custreelid)  {         // TODO Auto-generated method stub         return null;    }             /**     *  Call Pangu WMS System WebService Get reelid Info      *  return format is a string and need to parse it yourself        *  @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;     }        /**     *  Analysis Pangu WMS  webservice returns the Reelid string      *  format:<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 strings      *  @return  ReelIdVO      */    private reelidvo parsexmltovo (String  Reelidinfo)  {        system.out.println (reelIdInfo);         log.debug ("reelidinfo = "  + reelidinfo);         try {             documentbuilderfactory dbf = documentbuilderfactory.newinstance ();             DocumentBuilder builder =  Dbf.newdocumentbuilder ();             //  Adding XML Definitions              //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"));             //  Finding XML node data through XPath              Xpathfactory factory = xpathfactOry.newinstance ();             xpath xpath  = factory.newxpath ();             // Reelid element text under the first row under the dataset under the   root              Xpathexpression expr = xpath.compile ("/dataset/row[1]//*");             NodeList nodes =  (NodeList)  expr.evaluate (doc,  Xpathconstants.nodeset);            if  (null  == nodes)  {                 return null;            }                         &nBsp REELIDVO&NBSP;REELIDVO&NBSP;=&NBSP;NEW&NBSP;REELIDVO ();             //  Inventory Organization              Reelidvo.setorganizationid (Long.valueof (Getnodevaluebynodename (nodes,  "OrganizationId"));             // REELID             reelidvo.setreelid (Getnodevaluebynodename (nodes,  "REELID"));             //  Remaining Quantity              reelidvo.setremainqty (double.valueof (GetNodeValueByNodeName (nodes,   "QTY"));            //  Original quantity              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 calls return XML-formatted data webservice, and the application of XML parsing through XPath

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.