2013-12-20 48 views
0

嗨,我一直在嘗試這一段時間..我想解析Java類中的XML。但是爲了解析XML,我需要將它們傳遞給java類。我怎麼做? 這是在LISA中測試的一段代碼。如何將XML轉換爲字符串並將其傳遞給Web服務中的Java代碼?

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Body> 
    <addnumber xmlns="http://ejb3.examples.itko.com/"> 
     <firstnumber xmlns="">3</firstnumber> 
     <secondnumber xmlns="">22</secondnumber> 
    </addnumber> 
</soapenv:Body> 
</soapenv:Envelope> 

它運行在本地主機上目前,從那裏的值被傳遞給Java罐和結果得到的dislpayed。同樣的matchscript是

import Calc.Addition; //this the java package to import for addition 
String operationname = null; 
String operationname = incomingRequest.getOperation(); //incoming request refers to the above xml 
String i = incomingRequest.getArguments().get("firstnumber"); 
String j = incomingRequest.getArguments().get("secondnumber"); 
int ifirstNumber = Integer.parseInt (i); 
int jsecondNumber = Integer.parseInt (j); 

Addition myobj = new Addition(ifirstNumber,jsecondNumber); //values are passed to the constructor of class 

int Result = myobj.giveResult(); //function giveResult gives back the sum 
String sResponseXML = ""; 

sResponseXML =  
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" >\n"+ 
"<soap:Body> \n"+ 
"<p xsi:type=\"p:ServiceMessageObject\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema- instance\" xmlns:api=\"http://api.webservices.cc.guidewire.com/\" xmlns:util=\"http://UtilLibrary\" xmlns:xa=\"http://XactimateMediationLibrary\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n"+ 
"<AdditionResult>"+ Result +"</AdditionResult>\n"+ 
"</p>\n"+ 
"</soap:Body>\n"+ 
"</soap:Envelope>"; 
// Assigning value to the property RESPONSE_XML 
testExec.setStateValue("RESPONSE_XML",sResponseXML); 

這是成功地執行了正確的代碼。但現在我想以字符串的形式將整個上面的XML文件傳遞給JAVA jar。請幫忙。

+0

你已經在你的代碼片段中的字符串中傳遞了xml,所以我不明白你確切的問題是什麼? – eis

+0

我的問題是,我通過網絡服務來做這件事..而XML代碼不是直接調用Java類..它必須通過虛擬服務環境... – InCh

回答

0
import java.io.*; 
public class Check 
{ 
    public static void main(String [] args) 
    { 
     BufferedReader br = null; 
     try { 
      br= new BufferedReader(new FileReader(Fname)); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     String xmlinput; 

     StringBuffer sb= new StringBuffer(); 
     try { 
      while((xmlinput=br.readLine()) !=null) 
      { 
       sb.append(xmlinput.trim()); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     System.out.println(sb.toString()); 
    } 
} 

現在,您已將xml的內容設置爲字符串。

+0

謝謝你的解決方案:) – InCh

相關問題