简体   繁体   中英

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

Hi, I have been trying this for a while.. I want to parse the XML in Java Class. But to parse the XML i ll need to pass the same to the java Class. How do I do that? this is the piece of code that is tested in 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>

It runs on a local host currently, from where the values are passed to a java jar and result obtained is dislpayed. The matchscript of the same is

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 );

This is the correct code that is executed sucessfully. But now I want to pass the whole above XML file to the JAVA jar in the form of string. PLease help.

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());
    }
}

Now you have the content of your xml as string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM