cancel
Showing results for 
Search instead for 
Did you mean: 

Parse xml with UDF

Former Member
0 Kudos

Hi

I got the following XML stream

"

<?xml version="1.0" encoding="UTF-8"?><rfc:ZFI_CHECK_RATE.Response xmlns:rfc="urn:sap-com:document:sap:rfc:functions"><C_UKURS>4.20000</C_UKURS></rfc:ZFI_CHECK_RATE.Response>"

I want to write user defined function which will return me only the value of the C_UKURS node (4.20000)

any ideas?

thx,Shai

Accepted Solutions (1)

Accepted Solutions (1)

MichalKrawczyk
Active Contributor
0 Kudos

Hi,

do the search in the string for C_UKURS

and take the value from &gt to &lt

if it occurs only once

Regards,

michal

Former Member
0 Kudos

thx Michal for the answer

Ill try to do it

anyway, I am using more RFC Lookups

So I want to use XPath Or DOM for that

can u give me an example ?

Thx,Shai

bhavesh_kantilal
Active Contributor
0 Kudos

Shai,

Takea look at this blog and the code used for the parsing.

It uses DOM parser,

/people/alessandro.guarneri/blog/2006/03/27/sap-xi-lookup-api-the-killer

Regards,

Bhavesh

Former Member
0 Kudos

this weblog covers the whole process

I need only the DOM UDF

and u cant really understand it from the code

Former Member
0 Kudos

I wrote the following function

DOMParser parser = new DOMParser();

InputSource input = new InputSource(a);

parser.parse(input);

Document MyDoc = parser.getDocument();

String value = MyDoc.getDocumentElement().getFirstChild().getNodeValue();

return value;

and I get the following error

/usr/sap/XID/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Mapc468dbd0312c11dbbb63001125a56002/source/com/sap/xi/tf/_FI_CURR_MM_.java:258: unreported exception org.xml.sax.SAXException; must be caught or declared to be thrown parser.parse(input); ^ 1 error

any Ideas?

bhavesh_kantilal
Active Contributor
0 Kudos

Shai,

put the code in a try catch block,

try{
DOMParser parser = new DOMParser();
InputSource input = new InputSource(a);
parser.parse(input);
Document MyDoc = parser.getDocument();
String value = MyDoc.getDocumentElement().getFirstChild().getNodeValue(); 
}
catch(Exception e){
}

return value;

Regards,

Bhavesh

Message was edited by: Bhavesh Kantilal

Former Member
0 Kudos

thx Bhavesh for the answer

now IM getting the following error

usr/sap/XID/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Mape0e7c070312f11db91e7001125a56002/source/com/sap/xi/tf/_FI_CURR_MM_.java:265: cannot resolve symbol symbol : variable value location: class com.sap.xi.tf._FI_CURR_MM_ return value;

bhavesh_kantilal
Active Contributor
0 Kudos

Okie.. got it, This will solve the issue.

<i>String value;</i>
try{
DOMParser parser = new DOMParser();
InputSource input = new InputSource(a);
parser.parse(input);
Document MyDoc = parser.getDocument();
value = MyDoc.getDocumentElement().getFirstChild().getNodeValue(); 
}
catch(Exception e){
}
 
return value;

Regards,

Bhavesh

Former Member
0 Kudos

good job !!

I just changed the first line to String value = "" ;

and it worked!!!

But the function didnt return me the C_UKURS field

just an empty field

any Idea what to chane? I think its in the

value = MyDoc.getDocumentElement().getFirstChild().getNodeValue(); line

thx again,

Shai

bhavesh_kantilal
Active Contributor
0 Kudos

Shai,

Try this code,

String value;
try{
DOMParser parser = new DOMParser();
InputSource input = new InputSource(a);
parser.parse(input);
Document MyDoc = parser.getDocument();
// look for the tag 'C_UKURS'
<i>NodeList list = document.getElementsByTagName("C_UKURS");
Node node = list.item(0);
if (node != null) {
// if found, look for the value of 'C_UKURS'
node = node.getFirstChild();
if (node != null) {
value = node.getNodeValue();</i>
}catch(Exception e){
}
 
return value;

Regards,

Bhavesh

Former Member
0 Kudos

Hi

Still returns empty string

bhavesh_kantilal
Active Contributor
0 Kudos

Shai,

Can you give me the input XML to this Function.

Regards,

Bhavesh

Former Member
0 Kudos

u have it int he begging of the forum

btw I changed your code a bit to fix the errors

//write your code here

String value = "";

try{

DOMParser parser = new DOMParser();

InputSource input = new InputSource(a);

parser.parse(input);

Document MyDoc = parser.getDocument();

// look for the tag 'C_UKURS'

NodeList list = MyDoc.getElementsByTagName("C_UKURS");

Node node = list.item(0);

if (node != null) {

// if found, look for the value of 'C_UKURS'

node = node.getFirstChild();

}

if (node != null) {

value = node.getNodeValue();

}

}catch(Exception e){

}

return value;

bhavesh_kantilal
Active Contributor
0 Kudos

Shai,

Try this code,

String value="";
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// create DOM structure from input XML
DocumentBuilder builder = factory.newDocumentBuilder();
ByteArrayInputStream bais=new ByteArrayInputStream(<b>a.getBytes()</b>);
Document document = builder.parse(bais);
// look for the tag 'C_UKURS'
NodeList list = document.getElementsByTagName("C_UKURS");
Node node = list.item(0);
if (node != null) {
// if found, look for the value of 'C_UKURS'
node = node.getFirstChild();
}
if (node != null) {
value = node.getNodeValue();
}
}
catch(Exception e){
}
return value;

In the above code, I have assumed that "a" is the input to the User Defined Function and is a String containing the XML.

Regards,

Bhavesh

Former Member
0 Kudos

ok ill try to do it

which pacakges did u import?

Former Member
0 Kudos

well done

it worked

u deserve the points

Former Member
0 Kudos

Hello,

Does anyone know which packages to import for this to work?

Warm regards,

Glenn

Former Member
0 Kudos

Glenn,

Try this

org.w3c.dom.Document;com.sap.aii.mapping.lookup.*;org.w3c.dom.Node;org.w3c.dom.Element;javax.xml.parsers.DocumentBuilder;javax.xml.parsers.DocumentBuilderFactory;org.w3c.dom.NodeList;

Former Member
0 Kudos

Thanks dude! It worked! I wish I could give you points.

Former Member
0 Kudos

>>Thanks dude! It worked! I wish I could give you points.

I am happy with the former part

Answers (0)