cancel
Showing results for 
Search instead for 
Did you mean: 

RFC lookup parsing

Former Member
0 Kudos

Hi All,

I have wriiten a RFC lookup for mapping the company code. But I am not getting the result while parsing the XML. I have tested the code and without parsing I am getting the correct value in XML. Can anybody send the code for parsing the XML data using DOM.

Below is the code which I am using .

try {

docResponse = builder.parse(in);

if (docResponse == null) {

importanttrace.addWarning("docResponse is null");

}

res = docResponse.getElementsByTagName("COMPANYID").item(0).getFirstChild().getNodeValue();

if (res == null) {

importanttrace.addWarning("res is null");

}

}

catch (Exception e) {

importanttrace.addWarning("Error when parsing RFC Response - " + e.getMessage());

}

try {

// Free resources, close the accessor..

if (accessor != null) {

try {

accessor.close();

} catch (LookupException e) {

importanttrace.addWarning( "Error while closing accessor " + e.getMessage());

}

}

} catch (Exception e) {

importanttrace.addWarning("Result value not found in DOM - " + e);

}

// return the result obtained above

return res;

Thanks,

Aparna

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

It is throwing exception 'Error when parsing RFC response- null'. I have defined this exception under Catch in the code.

former_member187339
Active Contributor
0 Kudos

Hi Aparna,

You have defined the document builer

DocumentBuilder builder = null;

but have not initialized it (or did i miss that?). Try initializing it as

builder = factory.newDocumentBuilder();

and then call the builder.parse() fucntion

Regards

Suraj

Former Member
0 Kudos

I have initialized it in the beginning itself.

// Create document builder to create DOM XML document DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; factory.setNamespaceAware(false); factory.setValidating(false); try { // Create XML document using document builder builder = factory.newDocumentBuilder();

Also here is the XMLin the result which is to be parsed.

<bukrs><?xml version="1.0" encoding="UTF-8"?><rfc:ZFI_GET_COCODE.Response xmlns:rfc="urn:sap-com:document:sap:rfc:functions"><COMPANY_CODE>A105</COMPANY_CODE></rfc:ZFI_GET_COCODE.Response></bukrs>

former_member187339
Active Contributor
0 Kudos

Hi Aparna,

Check this:

1. Your response is having COMPANY_CODE and not COMPANYID

res = docResponse.getElementsByTagName("COMPANY_CODE").item(0).getFirstChild().getNodeValue();

2. why <bukrs> comes as start and end node of the result. If it is there then the result is not a valid XML message and you will get an error

The processing instruction target matching "[xX][mM][lL]" is not allowed.

If you make the above changes your parsing code will work

Regards

Suraj

Answers (3)

Answers (3)

Former Member
0 Kudos

Thanks a lot Suraj for your help. I changed the tag name from 'COMPANYID' to 'COMPANY_CODE' and it worked.

Former Member
0 Kudos

Here is the complete code.

Imports com.sap.aii.mapping.lookup.;java.util.Iterator;java.util.Map;java.io.;javax.xml.parsers.;org.w3c.dom.;

public String RFC_lookup_test(String a,Container container){

//write your code here

final String LIST_CONTENT_TAG = "company_id",

VALUE_NOT_FOUND = ""; // Default return value in case something goes wrong

String content = "";

MappingTrace importanttrace;

importanttrace = container.getTrace();

// Create document builder to create DOM XML document

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = null;

factory.setNamespaceAware(false);

factory.setValidating(false);

try {

// Create XML document using document builder

builder = factory.newDocumentBuilder();

} catch (Exception e) {

importanttrace.addWarning("Error creating DocumentBuilder - " + e.getMessage());

return null;

}

// filling the string with our RFC-XML (with values)

String m ="<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns1:ZFI_GET_COCODE xmlns:ns1=\"urn:sap-com:document:sap:rfc:functions\"><COMPANYID>" + a + "</COMPANYID></ns1:ZFI_GET_COCODE>";RfcAccessor accessor = null;

ByteArrayOutputStream out = null;

Payload result = null;

try

{

// 1. Determine a channel (Business system, Communication channel)

Channel channel = LookupService.getChannel("BS_SAP_DEC210","CC_RECEIVER_RFC_test");

// 2. Get a RFC accessor for a channel.

accessor = LookupService.getRfcAccessor(channel);

// 3. Create a xml input stream representing the function module request message.

InputStream inputStream = new ByteArrayInputStream(m.getBytes());

// 4. Create xml payload

XmlPayload payload = LookupService.getXmlPayload(inputStream);

// 5. Execute lookup

result = accessor.call(payload);

/*InputStream in = result.getContent();

out = new ByteArrayOutputStream(1024);

byte[] buffer = new byte[1024];

for (int read = in.read(buffer); read > 0; read = in.read(buffer)) {

out.write(buffer, 0, read);

}

content = out.toString();

*/

if (result == null) {

importanttrace.addWarning("result of RFC call is null");

}

}

catch(LookupException e)

{

importanttrace.addWarning("Error while lookup " + e.getMessage() );

}

Document docResponse = null;

InputStream in = result.getContent();

String returnValue = VALUE_NOT_FOUND;

String res = " ";

try {

docResponse = builder.parse(in);

if (docResponse == null) {

importanttrace.addWarning("docResponse is null");

}

res = docResponse.getElementsByTagName(u201CCOMPANYIDu201D).item(0).getFirstChild().getNodeValue();

if (res == null) {

importanttrace.addWarning("res is null");

}

}

catch (Exception e) {

importanttrace.addWarning("Error when parsing RFC Response - " + e.getMessage());

}

try {

// Free resources, close the accessor..

if (accessor != null) {

try {

accessor.close();

} catch (LookupException e) {

importanttrace.addWarning( "Error while closing accessor " + e.getMessage());

}

}

} catch (Exception e) {

importanttrace.addWarning("Result value not found in DOM - " + e);

}

// return the result obtained above

return res;

former_member187339
Active Contributor
0 Kudos

Hi Aparna,

What message are you getting when you used your parsing code? Also can you paste here the result (xml result) of the RFC lookup. It will be helpful in writing the parsing cope

Regards

Suraj