cancel
Showing results for 
Search instead for 
Did you mean: 

Help regarding uploading an XML file into a Java Program

Former Member
0 Kudos

Hi All

I need to upload an XML file into a Java Program and pass the data to SAP for calling a BAPI. I am able to parse through the XML file but unable to fetch the node name and node value.

Can anyone give the code which will serve this purpose?

Thanks in Advance,

Sreeramya.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Sree

Have the look at the following sample program. This program process Fibonacci Series.

XML File:

<?xml version="1.0"?>

<methodResponse>

<params>

<param>

<value><double>28657</double></value>

</param>

</params>

</methodResponse>

*****************************************

Prgram :

import java.net.*;

import java.io.*;

import java.math.BigInteger;

public class FibonacciClient {

static String defaultServer

= "http://www.elharo.com/fibonacci/XML-RPC";

public static void main(String[] args) {

if (args.length <= 0) {

System.out.println(

"Usage: java FibonacciClient number url"

);

return;

}

String server = defaultServer;

if (args.length >= 2) server = args[1];

try {

// Connect to the server

URL u = new URL(server);

URLConnection uc = u.openConnection();

HttpURLConnection connection = (HttpURLConnection) uc;

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

OutputStream out = connection.getOutputStream();

Writer wout = new OutputStreamWriter(out);

// Write the request

wout.write("<?xml version=\"1.0\"?>\r\n");

wout.write("<methodCall>\r\n");

wout.write(

" <methodName>calculateFibonacci</methodName>\r\n");

wout.write(" <params>\r\n");

wout.write(" <param>\r\n");

wout.write(" <value><int>" + args[0]

+ "</int></value>\r\n");

wout.write(" </param>\r\n");

wout.write(" </params>\r\n");

wout.write("</methodCall>\r\n");

wout.flush();

wout.close();

// Read the response

InputStream in = connection.getInputStream();

BigInteger result = readFibonacciXMLRPCResponse(in);

System.out.println(result);

in.close();

connection.disconnect();

}

catch (IOException e) {

System.err.println(e);

}

}

private static BigInteger readFibonacciXMLRPCResponse(

InputStream in) throws IOException, NumberFormatException,

StringIndexOutOfBoundsException {

StringBuffer sb = new StringBuffer();

Reader reader = new InputStreamReader(in, "UTF-8");

int c;

while ((c = in.read()) != -1) sb.append((char) c);

String document = sb.toString();

String startTag = "<value><double>";

String endTag = "</double></value>";

int start = document.indexOf(startTag) + startTag.length();

int end = document.indexOf(endTag);

String result = document.substring(start, end);

return new BigInteger(result);

}

}

Compare it with your especially on Node Processing..

This should help you !!!!

Thanks

Jack

<b>Allot points if my post helps !!!</b>

Message was edited by: Jack