cancel
Showing results for 
Search instead for 
Did you mean: 

Creating RFC request in java

Former Member
0 Kudos

Hi.

I have a small issue, creating a RFC call from java.

To be specific, I'm updating a table in the backend, using an adapter module.

When creating the request using DOM, I get this error:

Exception:

Exception: error while processing message to remote system:

com.sap.aii.af.rfc.core.client.RfcClientException: could not get functionname from XML requst:

com.sap.aii.af.rfc.RfcAdapterException: failed to read funtionname from XML document: missing namespace declaration(2).

I've seen a few places in the forum, that there's a simular problem in UDF, where you have to create the request string in one line. Else you get the same error.

The question is, what is the difference between these two functions, and why is the DOM function failing when the other one works?

The request should look like this:

<?xml version="1.0" encoding="UTF-8"?>

<rfc:Z_MON_SURV_UPDATE xmlns:ns1="urn:sap-com:document:sap:rfc:functions">

<ID>1</ID>

</rfc:Z_MON_SURV_UPDATE>

Function 1:

private NodeList buildRequestXml(String id) {

try {

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

Node docRoot = doc.appendChild(doc.createElementNS("urn:sap-com:document:sap:rfc:functions","rfc:Z_MON_SURV_UPDATE"));

Element ID = doc.createElement("ID");

docRoot.appendChild(ID).appendChild(doc.createTextNode(id));

return doc.getChildNodes();

} catch (Exception e) {

throw new RuntimeException("Error while building request XML", e);}

}

Function 2:

private NodeList buildRequestXml(String id) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {

String res = "<rfc:Z_MON_SURV_UPDATE xmlns:rfc=\"urn:sap-com:document:sap:rfc:functions\"><ID>"id"</ID></rfc:Z_MON_SURV_UPDATE>";

Document XMLDoc= DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(res)));

return XMLDoc.getChildNodes();

}

Regards...

Peter

Accepted Solutions (1)

Accepted Solutions (1)

aashish_sinha
Active Contributor
0 Kudos

Hi,

Can you try something like this, just for an idea.

Element rootEle = outdoc.createElement("ns0:MT_Student_Receiver1");

rootEle.setAttribute("xmlns:ns0","http:

simplejavamapping_avg.com");

outdoc.appendChild(rootEle);

System.out.println("Root element added");

Regards

Aashish Sinha

PS : reward points if helpful

Former Member
0 Kudos

Hi Aashish.

Your right, thanks.

Just tried the something like, what you suggest.

In my case the result method looks like this:

private NodeList buildRequestXml(String id) {

try {

Document doc =

DocumentBuilderFactory

.newInstance()

.newDocumentBuilder()

.newDocument();

Node docRoot = doc;

Element Z_MON_SURV_UPDATE = doc.createElement("rfc:Z_MON_SURV_UPDATE");

Z_MON_SURV_UPDATE.setAttribute("xmlns:ns1","urn:sap-com:document:sap:rfc:functions");

docRoot = doc.appendChild(Z_MON_SURV_UPDATE);

Element ID = doc.createElement("ID");

docRoot.appendChild(ID).appendChild(doc.createTextNode(id));

return doc.getChildNodes();

} catch (Exception e) {

throw new RuntimeException("Error while building request XML", e);

}

}

Regards...

Peter

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

I think the problem is that you try to alter the namespace referenceres in the line.

Node docRoot = doc.appendChild(doc.createElementNS("urn:sap-com:document:sap:rfc:functions","rfc:Z_MON_SURV_UPDATE"));

change to

Node docRoot = doc.appendChild(doc.createElementNS("urn:sap-com:document:sap:rfc:functions","Z_MON_SURV_UPDATE"));

Former Member
0 Kudos

Hi Daniel.

Good point, but unfortunately it didnt work.

Any other suggestions?

Regards Peter