1. 程式人生 > 其它 >java 寫webservice介面解析xml報文

java 寫webservice介面解析xml報文

1        <!--解析xml報文-->
2         <dependency>
3             <groupId>dom4j</groupId>
4             <artifactId>dom4j</artifactId>
5             <version>1.6.1</version>
6         </dependency>    

新增一個介面

1 @WebService
2 public interface IAPP_WS_002_insertUserDateService {
3 4 public void insertUserDate(String body); 5 6 }

實現類

 1 import com.webservice.server.service.IAPP_WS_002_insertUserDateService;
 2 import org.dom4j.Document;
 3 import org.dom4j.DocumentException;
 4 import org.dom4j.DocumentHelper;
 5 import org.dom4j.Element;
 6 
 7 import javax.jws.WebService;
8 import java.util.Iterator; 9 10 /** 11 * 插入人員資料 12 */ 13 @SuppressWarnings("all") 14 @WebService 15 public class APP_WS_002_insertUserDateServiceImpl implements IAPP_WS_002_insertUserDateService { 16 17 /** 18 * 解析傳來的xml報文 19 * 20 * @param body 21 */ 22 @Override 23 public
void insertUserDate(String body) { 24 readStringXml(body); 25 } 26 27 28 /** 29 * 解析傳來的xml報文 30 * 31 * @param xml 32 */ 33 public static void readStringXml(String xml) { 34 Document doc = null; 35 try { 36 doc = DocumentHelper.parseText(xml); // 將字串轉為XML 37 Element rootElt = doc.getRootElement(); // 獲取根節點 38 System.out.println("根節點:" + rootElt.getName()); // 拿到根節點的名稱 DATA 39 Iterator iter = rootElt.elementIterator("HEAD"); // 獲取根節點下的子節點HEAD 40 // 遍歷HEAD節點 41 while (iter.hasNext()) { 42 Element recordEle = (Element) iter.next(); 43 String BIZTRANSACTIONID = recordEle.elementTextTrim("BIZTRANSACTIONID"); //取HEAD下節點的值 44 String COUNT = recordEle.elementTextTrim("COUNT"); //取HEAD下節點的值 45 String CONSUMER = recordEle.elementTextTrim("CONSUMER"); //取HEAD下節點的值 46 String SRVLEVEL = recordEle.elementTextTrim("SRVLEVEL"); //取HEAD下節點的值 47 String ACCOUNT = recordEle.elementTextTrim("ACCOUNT"); //取HEAD下節點的值 48 String PASSWORD = recordEle.elementTextTrim("PASSWORD"); //取HEAD下節點的值 49 50 System.out.println("BIZTRANSACTIONID:" + BIZTRANSACTIONID); 51 System.out.println("COUNT:" + COUNT); 52 System.out.println("CONSUMER:" + CONSUMER); 53 System.out.println("SRVLEVEL:" + SRVLEVEL); 54 System.out.println("ACCOUNT:" + ACCOUNT); 55 System.out.println("PASSWORD:" + PASSWORD); 56 } 57 Iterator iterss = rootElt.elementIterator("LIST"); ///獲取根節點下的子節點LIST 58 // 遍歷LIST節點 59 while (iterss.hasNext()) { 60 Element recordEless = (Element) iterss.next(); 61 Iterator itersElIterator = recordEless.elementIterator("ITEM"); // 獲取子節點LIST下的子節點ITEM 62 // 遍歷LIST節點下的ITEM節點 63 while (itersElIterator.hasNext()) { 64 Element itemEle = (Element) itersElIterator.next(); 65 String ID = itemEle.elementTextTrim("ID"); 66 String NAME = itemEle.elementTextTrim("NAME"); 67 String ADDR = itemEle.elementTextTrim("ADDR"); 68 String SEX = itemEle.elementTextTrim("SEX"); 69 String AGE = itemEle.elementTextTrim("AGE"); 70 String MARK = itemEle.elementTextTrim("MARK"); 71 72 System.out.println("ID:" + ID); 73 System.out.println("NAME:" + NAME); 74 System.out.println("ADDR:" + ADDR); 75 System.out.println("SEX:" + SEX); 76 System.out.println("AGE:" + AGE); 77 System.out.println("MARK:" + MARK); 78 } 79 } 80 } catch (DocumentException e) { 81 e.printStackTrace(); 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 } 86 }

釋出

 1 import com.webservice.server.service.impl.APP_WS_001getNoteByIdServiceImpl;
 2 import com.webservice.server.service.impl.APP_WS_002_insertUserDateServiceImpl;
 3 import org.springframework.boot.ApplicationArguments;
 4 import org.springframework.boot.ApplicationRunner;
 5 import org.springframework.stereotype.Component;
 6 import javax.xml.ws.Endpoint;
 7 
 8 @Component
 9 @SuppressWarnings("all")
10 public class WebserviceConfig implements ApplicationRunner {
11 
12    
13     private String add_WS_002 = "http://localhost:8081/webservice/insertUserDate";
14 
15     @Override
16     public void run(ApplicationArguments args) throws Exception {
17         Endpoint.publish(add_WS_002, new APP_WS_002_insertUserDateServiceImpl());
18         System.out.println("webservice介面 釋出成功");
19     }
20 }

測試

使用SOAPUI測試

後臺獲取如下