cancel
Showing results for 
Search instead for 
Did you mean: 

How to insert <?xml version="1.0" encoding="utf-8"?> before an element

Former Member
0 Kudos

Hi, I have a message which must contain XML inside XML, this internal one must start off with the <?xml version="1.0" encoding="utf-8"?> string.

Is there a nice trick for inserting this before an element? (not at the beginning of the mapping, a specific place further inside the XML).

All of the other elements are mapped as part of the message type.

Accepted Solutions (1)

Accepted Solutions (1)

sunilchandra007
Active Contributor
0 Kudos

Did you check .

You can do this easily with UDF.

public String setPrevalue(String var1,Container container) {


     StructureNode node = ((StructureNode) container.getParameter("STRUCTURE_NODE"));

     node.setPreValue("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

     return""

                                

}

Former Member
0 Kudos

Great blog entry Sunil, those functions are very powerful!!!

Thanks for the help, worked like a charm.

Answers (1)

Answers (1)

Former Member
0 Kudos

You can write a java mapping to add the string anywhere in the xml you need.

Former Member
0 Kudos

Yeah thanks Osman, but then I think it would be like the text inside an element. Which would be fine I think if there were no sub-elements. In this case there are sub-elements so XML rules will not let string data be put in correct?

Data structure is like:

<MySOAPBodyRoot>

<element1></element1>

<element2></element2>

<element3>

   [ here is where I want to insert the XML string ]

   <subElement1> [ this is like payload root of inside XML ]

      <subSubElement></subSubElement>

   </subElement1>

</element3>

</MySOAPBodyRoot>

JaySchwendemann
Active Contributor
0 Kudos

Hmm thinking while I'm typing: What about using any raw data?

Of course your recipient must be able to deal with the encoded data, I suppose

Cheers

Jens

former_member192851
Active Participant
0 Kudos

You can implement this logic in Java Mapping:

1. Get input xml.

2. Convert xml to Strings.

3. Find <element3> string.

4. Add whatever you want

5. Return result.

Former Member
0 Kudos

Hi Artem, we aren't really set up for developing / updating Java mappings. Do you know of a way to accomplish using Java UDF or Graphical Mapping?

Thanks Jens, we have to confirm to the 3rd party here, they will not modify anything for us.

Former Member
0 Kudos

Create a 2nd mapping.

Use the target of 1st mapping as source and

Create new DT with just the record node and use at target.

Do a mapping with the header node of Source to Target with the UDF in between(code below) and select "Return as xml" on source node.

Note: payload is the input for the UDF

  int i = payload.indexOf("<subElement1>");

  String a = payload.substring(0,i);

  String b = payload.substring(i);

  String output = a + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +b;

return output;

JaySchwendemann
Active Contributor
0 Kudos

Hi Aaron,

correct me if I'm wrong but inserting an additional <?xml... statement within an XML document would render the whole document not-well-formed. While your 3rd party receiver may ignore this I would assume PI does not (easily).

However, never tried this. If Soap Adapter fails I would try with HTTP (depends on your scenario, though).

You got interesting tasks going on there

Cheers

Jens