cancel
Showing results for 
Search instead for 
Did you mean: 

Ampersand ('&') in XML - Mapping error

Former Member
0 Kudos

My Scenario is File to File. XI ( we are PI7.0) receives XML File and I'm splitting ( transorming ) into multiple files in Mapping. Scenario is failing because data contains '&' ( for example K&K Company ). Even XMLFox or XMLSpy gives error saying & is illegal character. Do I have to use encoding 8859-1 instead of utf-8 ? Does that solves my problem ?. Or Do I have to setup or change JDK ?

Can someone help me what I have to do to fix this error?

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Sorry..I was not able to reward points. I can not see tabs to award points as we used in SDN forums earlier. How Can I award points ?

stefan_grube
Active Contributor
0 Kudos

The character & is not allowed in XML. The program which creates your XML file has to provide & instead. If you cannot change the sender, than you have to write a Java mapping program which escapes the &

Regards

Stefan

Former Member
0 Kudos

Stefan,

thanks for the reply. Would you please guide me the Mapping required to escape this character please ?

Former Member
0 Kudos

Hi,

As pointed out by Stefan, you need to go for JAVA mapping to escape & symbol. Another work around is to have an adapter module at sender side that will convert & to & in your sender file adapter and the reverse process at reciver adapter.

Regards,

Jai Shankar

Former Member
0 Kudos

Jai, Thank you for suggession. As I donot have NetWeaver Developer Studio, How can I do that. Is there any standard routine available to use ? ( I mean .jar file ). Would you please guide me.

Instead of Java Mapping can I do that using Graphical Message mapping ?

My Input XML will look like

<Start_rec>

<Company>

<Name>abc&abc</Name>

<Name>xyz</Name>

..........

</Company>

</Start_rec>

Since its only single record ( 0.. unbounded) Is it possible If I can do that using Graphical Message Mapping ?.. Thanks a lot for any insights ..

Former Member
0 Kudos

In case its only a single record then you can use the string funtctions in XI.

First use Boolean function to check whether it is <b>&</b>, If yes,then replace with the string <b>&amp;</b>

In case it is not a single record, then you have to use Java mapping, and you dont need NWDS for it. you can alwys use a textpad or an eclipse to do it. you will get lots of help on net for this.

Former Member
0 Kudos

the replacement string did not show as i typed it.

replace & with '& amp ;'

and dont keep any gaps in between & and amp and ;

stefan_grube
Active Contributor
0 Kudos

But don't forget:

If the &amp; starts an escape string like &amp;gt; then you should not replace &amp; with &amp;amp;

It is not so easy.

S T: Is there no possibility to change the sender to provide and escaped &amp;? It is definitively wrong to have an &amp; in an XML.

Regards

Stefan

Former Member
0 Kudos

Stephan,

Sending system is a legacy system and they are genearting XML file. & character is very common in names of companies or stores etc etc. I believe its not fair to ask them ( Its Legacy system )to change . They wont agree for example 'Procter&Gample' to 'Procter& amp ; Gamble' . Let me try what Vijaya is suggested.

Message was edited by:

S T

Former Member
0 Kudos

Vijaya,

how can I use Boolean Function?. & char can be anywhere in the string . Please help.

stefan_grube
Active Contributor
0 Kudos

I think it is fair enough to ask the guys from legacy system to provide a <b>valid</b> XML structure. Maybe they have not thought about this by know

Regards

Stefan

Former Member
0 Kudos

Stephan,

actually this input message consists of multiple files wrapped with XML tags genearated around each record. XI splits these file into multiple single files.

To Be exact it will be like this

<Files>

<File1>

<File1Rec>K&K Company</File1Rec1>

<File1Rec>abcd&abcd</File1Rec>

</File1>

<File2>

<File2Rec>itsnot fair</File2Rec>

</File2>

<File3>

<File3Rec>its fair</File3Rec>

</File3>

<File4>

</File4>

All 4 files above are 0...unbounded

We are doing this since we donot want to deliver one before the other as they will processed together in dest system as they are related to each other. This is the reason I'm not doing content conversion on the Inbound side as this message comes with XML tags wrapped around.

XI uses BPM to split into 4 files.

Content conversion on the outbound side and finally each file will be in csv format.

In Short they donot want to do convert & to "& amp ;" simply because they dont know what xml is.

So I need to figure out within XI itself to tackle this issue. Thx

</Files>

stefan_grube
Active Contributor
0 Kudos

When you are sure, that no escape sequence is inside the XML, then you can easily replace all &amp; to &amp;amp;.

Here is a sample code:

    public void execute (InputStream in, OutputStream out)
        throws StreamTransformationException {
        try {
            int c;
            while ((c = in.read()) != -1) {
                if (c != '&amp;') {
                    out.write(c);
                } else {
                    // ampersand
                    out.write("&amp;amp;".getBytes());
                }
            } // while
            out.flush();
        } catch (Exception e) {
            throw new StreamTransformationException(e.getMessage(),e);
        }
    }

Check also this blog:

/people/stefan.grube/blog/2006/10/23/testing-and-debugging-java-mapping-in-developer-studio

Hope that helps,

Stefan

Former Member
0 Kudos

Stephan,

So I have to use Java mapping in IM ? . I'm now installing NWDS. I have to create rar file with NWDS and import into XI as import archive and then use it in IM before my mapping ? .

Former Member
0 Kudos

To escape HTML/XML contents, I suggest you to use commons-lang library from Apache. You can download it from <a href="http://jakarta.apache.org/site/downloads/downloads_commons-lang.cgi">here</a>.

Just call the StringEscapeUtils.escapeXml method in your Java Mapping.

Former Member
0 Kudos

Thanks Kenny,

I looked at the link you provided. Which One I should download? and How can I include the code to escape all Special Characters in my Input XML. Would you please guide me ?

Former Member
0 Kudos

Just download the 2.2.zip file. After that, unzip it and include commons-lang.jar in your mapping's classpath.

Then do something like this in your Java mapping:


public void execute (InputStream in, OutputStream out)
  throws StreamTransformationException {
  try {
    out.write(StringEscapeUtils.escapeXml(in.read_string()).getBytes());
  }
}

After this, your message is properly escaped, you can use graphical mapping or another Java mapping to finish the work. To test in XI, make sure you upload commons-lang.jar file into imported archive. This library will be available to your mapping class.

Hope this will help.

Former Member
0 Kudos

I donot have IDE like NWDS. Would I be able to do it Without IDE ? . I hate to ask this as I'm not java developer. Appreciate your help.

Former Member
0 Kudos

You can use notepad, emacs/vim, or any editor you like. Once you finish, use javac command line to compile it. Then upload the class file to XI. IDE just makes things a little easier for you.

Former Member
0 Kudos

Thanks a lot for your patience and help. Appreciate it. I would like to award points to Couple of people on this board but I was not able to award them for some reason. Can someone help me ? There are no radio buttons to award points.

Message was edited by:

S T

former_member342243
Participant
0 Kudos

Can any one please help me from where should i download that 2.2.zip file.

Please share link here.