繁体   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