cancel
Showing results for 
Search instead for 
Did you mean: 

XML parsing in UDF

Former Member
0 Kudos

Hi everyone!

First, I must say that I don't know anything about java/XML programming(I'm sorry)!

I'm just looking for some guiding lights for the following;

In the Message Mapping (IB), I'm trying to do a RFC Lookup to R/3 using a BAPI call. I followed Michal Krawczyk's "Mapping lookups - RFC API:

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/a03e7b02-eea4-2910-089f-8214c6d1...

"

And now I need to parse the XML response and retrieve a value from it!

I would like to know if the following portion of code is ok and wheere should I put it?

Code:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

// Create DOM structure

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(content);

// Lets find my tag

NodeList list = document.getElementsByTagName("Number");

Node node = list.item(0);

if (node != null) {

node = node.getFirstChild();

}

if (node != null) {

tagValue = node.getNodeValue();

}

Thanks a lot for your help!

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Thank you for the links!!!

I've consulted all of them and when I try to run the code, I'm getting the following message:

unreported exception org.xml.sax.SAXException; must be caught or declared to be thrown document = builder.parse(result.getContent());

Could you help me fix that issue?

Here's the code in question

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

"<ns0:BAPI_CUSTOMER_FIND""xmlns:ns0=""urn:sap-com:document:sap:rfc:functions>"+"<RESULT_TAB></RESULT_TAB><SELOPT_TAB><item><COMP_CODE>1000</COMP_CODE><TABNAME>KNA1</TABNAME><FIELDNAME>SORTL</FIELDNAME><FIELDVALUE>004258</FIELDVALUE></item></SELOPT_TAB></ns0:BAPI_CUSTOMER_FIND>";

RfcAccessor accessor = null;

ByteArrayOutputStream out = null;

importanttrace = null;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = null;

factory.setNamespaceAware(false);

factory.setValidating(false);

Document document = null;

try

{

builder = factory.newDocumentBuilder();

} catch (Exception e) {

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

return null;

}

try

{

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

Channel channel = LookupService.getChannel ("BS_FSCM_AR_CLNT100","CC_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(document.toString().getBytes());

//4. Create xml payload.

XmlPayload payload = LookupService.getXmlPayload(inputStream);

//5. Execute lookup.

Payload result = accessor.call(payload);

document = builder.parse(result.getContent());

//InputStream in = result.getContent();

out = new ByteArrayOutputStream(1024);

byte [] buffer = new byte[1024];

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

out.write(buffer, 0, read);

}

content = out.toString();

Thanks for your help!

Former Member
0 Kudos

Tersant,

You can cross check your code as mentioned in this weblog:

/people/bhavesh.kantilal/blog/2006/11/20/webservice-calls-from-a-user-defined-function

Regards,

---Satish

Former Member
0 Kudos

Tersant,

You can also get the value like mentioned in the weblog:

/people/wojciech.gasiorowski/blog/2006/11/01/the-power-of-regular-expressions-in-graphical-mapping-xi

Check point 5.

Regards,

---Satish