cancel
Showing results for 
Search instead for 
Did you mean: 

XML Parsing using Graphical Mapping in UDF

Former Member
0 Kudos

Hi Experties,

Can we Use JAXP to both parse and emit XML in Graphical mapping's UDF. If we import the jar files required for XML Parsing. can we do it in user-defined function.

After RFC Lookup if the lookup fails a mail should be triggered and want the resultant XML to be parsed and want to store the in a resultset. Is this possible through UDF. Can you send me the code.

I am referring the blogs

Mapping Lookups - RFC API by michal and

/people/thorsten.nordholmsbirk/blog/2006/08/10/using-jaxp-to-both-parse-and-emit-xml-in-xi-java-mapping-programs for javamapping

can i do the parsing also in the userdefined function where i am developing the lookup logic

Regards,

Chand...

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

hi govada,

as from your code you are getting the output from the RFC at line

InputStream in = result.getContent();

after that you can put the code that Bhavesh sent.

and i think the below code segment is not necessay in your code

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

}

i dont think you have to add any jar files for this, but you might have to import org.w3c.dom.*; to your udf. check it out.

kind regards

francis

Answers (1)

Answers (1)

bhavesh_kantilal
Active Contributor
0 Kudos

><i>can i do the parsing also in the userdefined function where i am developing the lookup logic</i>

You sure can.

Just use the following piece of code as the starting point,

Assuming rfcOutPayload is the variable in which the output of the lookup is stored,


InputStream inp = rfcOutPayload.getContent();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/* Create DOM structure from input XML */
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inp);
/* Assuming TAGNAME  is the TAG in which the Output is in the Response XML */
NodeList list = document.getElementsByTagName("TAGNAME");
Node node = list.item(0);
if (node != null) {
node = node.getFirstChild();
if (node != null) {
String tagValue = node.getNodeValue();
}

><i>After RFC Lookup if the lookup fails a mail should be triggered</i>

Trigger an Alert from your UDF and this will in turn trigger the mail . For triggering alerts from an UDF, my blog should help,

/people/bhavesh.kantilal/blog/2006/07/25/triggering-xi-alerts-from-a-user-defined-function

Regards

Bhavesh

Former Member
0 Kudos

what are the jar files i have to import to execute this code in my UDF.

this is the code in UDF i am getting in before... after parsing shall it give proper ouput without & and > <

//write your code here

String content = "";

MappingTrace importanttrace;

importanttrace = container.getTrace();

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

String m = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:BAPI_GOODSMVT_CREATE xmlns:ns0=\"urn:sap-com:document:sap:rfc:functions\"><GOODSMVT_CODE></GOODSMVT_CODE><GOODSMVT_HEADER></GOODSMVT_HEADER><GOODSMVT_ITEM><item><PLANT></PLANT></item></GOODSMVT_ITEM><RETURN></RETURN></ns0:BAPI_GOODSMVT_CREATE>";

RfcAccessor accessor = null;

ByteArrayOutputStream out = null;

try

{

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

Channel channel = LookupService.getChannel("BS_JKH_SAPR3_D","BAPI_in_returnsconfirmation");

// 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.

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

}

catch(LookupException e)

{

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

}

catch(IOException e)

{

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

}

finally

{

if (out!=null) {

try {

out.close();

} catch (IOException e) {

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

}

}

// 7. close the accessor in order to free resources.

if (accessor!=null) {

try {

accessor.close();

} catch (LookupException e) {

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

}

}

}

//returning the result – RFC-XML.response

return content;

Can u please suggest where i have to add u r peice of code...

i add in try block which resulting in errors....

Regards,

Chand...

null

henrique_pinto
Active Contributor
0 Kudos

chand,

check the getContent() method.

That's the link between both code pieces.

Regards,

Henrique.