cancel
Showing results for 
Search instead for 
Did you mean: 

Read XML tags dynamically

Former Member
0 Kudos

Hi

I have to read the XML dynamically , please see the input and expected output

Input:

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

                                                + "<records xsi:type=\"Opportunity\">"

                                                + "<Id>ABCD</Id>"

                                                + "<Amount>1000.0</Amount>" + "</records>";

Output

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

< data>

  <Object name> Opportunity  </Object name>

   <records>

     <FiledName>ID</FiledName>

     <FiledValue>ABCD</FiledValue>

   <records>

   <records>

     <FiledName>Amount</FiledName>

     <FiledValue>1000.0</FiledValue>

   <records>

< /data>

Please note that  the tags Opportunity, ID,Amount etc are dynamic.

Can you please tell how to read this using DOM/SAX or any other method?

Accepted Solutions (1)

Accepted Solutions (1)

udo_martens
Active Contributor
0 Kudos

Hi Sam,

pretty easy with xsl

The loop with: xsl:for-each "select=//records" (read all records elements) and

xsl:for-each "select="./*" (read all children of a records-element)

Read field values with normal xsl:value-of

Read field names with xsl:value-of select="local-name()"

/Udo

Former Member
0 Kudos

Thanks Udo.

But i have to use Java mapping because i am handling Soap Fault as well.

If it is only response then i can use XSLT as u suggested. But i need to handle both Reponse and Fault.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

As Udo mentioned xsl mapping is easier.  If you want to know how to approach using java mapping. I might want you to see this link.. Your requirement is pretty simpler.  Here the example uses sax parser.  You will find the code sample which is for beginner to understand like how to retrieve each tag element and how to construct new tag element for the output. If you could tweak the code , you will be able to make it. If you are new to java  development, take help from the java team.

http://www.riyaz.net/sap/xipi-java-mapping-demystified/415/

udo_martens
Active Contributor
0 Kudos

Hi,

i have to use Java mapping because i am handling Soap Fault as well.

Soap and therefore as well Soap Fault is xml. And you can access xml with xsl. No need for Java from my point of view. Java is good for special requirements, but xsl is easier and provides less effort.

/Udo

Answers (2)

Answers (2)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Sam,

              How are you receiving this input? Is it part of XML payload ? Is this the entire XML you are receiving? This needs small java mapping code to convert it to target XML.

Regards

Anupam 

Former Member
0 Kudos

Experts ..can you please give your suggestions?