簡體   English   中英

如何將XML轉換為字符串並將其傳遞給Web服務中的Java代碼?

[英]How to convert a XML to a String and pass the same to a java code in web service?

嗨,我已經嘗試了一段時間了。我想解析Java類中的XML。 但是要解析XML,我需要將其傳遞給java類。 我怎么做? 這是在LISA中測試過的代碼。

<?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 jar,並顯示獲得的結果。 的匹配項是

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。 請幫忙。

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的內容作為字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM