cancel
Showing results for 
Search instead for 
Did you mean: 

delete namespace and prefix with Seeburger X.400 receiver

peter_wallner2
Active Contributor
0 Kudos

Dear experts,

I have a problem deleting a namespace and prefixes "ns0" from an XML output. When testing I was using an FTP receiver adapter with the AnonymizerBean which worked perfectly. I referred to: /people/stefan.grube/blog/2007/02/02/remove-namespace-prefix-or-change-xml-encoding-with-the-xmlanonymizerbean

Now I have a Seeburger X.400 receiver adapter and the AnonymizerBean does not work anymore.

The output message looks like this:


<?xml version="1.0" encoding="UTF-8" ?> 
<ns0:ORDERRESPONSE xmlns:ns0="http://www.opentrans.org/XMLSchema/1.0" version="1.0">
<ns0:ORDERRESPONSE_HEADER>
<ns0:ORDERRESPONSE_INFO>
<ns0:ORDER_ID>xxxxx</ns0:ORDER_ID> 
....

So I need to get rid of the namespace and the prefixes "ns0". Does anyone know how to configure the module of the X.400 receiver adapter to do this?

Thank you very much for your help!

Best regards,

Peter

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Yes, an ugly problem. If the receiver side cannot handle those namespaces, I use to remove the namespaces with an additional XSLT or Java mapping.

CSY

peter_wallner2
Active Contributor
0 Kudos

Hello Christian,

I had tried the following XSLT before:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<xsl:template match="/">
   <xsl:copy-of select="ns0:ORDERRESPONSE" />
</xsl:template>
</xsl:stylesheet>

But it did not work right away so I went for the AnonymizerBean - unfortunately...

Best regards,

Peter

shweta_walaskar2
Contributor
0 Kudos

Hello Peter,

Try this:

import java.io.*;

import java.util.*;

import com.sap.aii.mapping.api.*;

public class RemoveNamespace implements StreamTransformation {

String record=null;

String comment=null;

private Map param = null;

private AbstractTrace trace = null;

public void execute(InputStream in, OutputStream out) {

try {

trace = (AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE );

DataInputStream stdin = new DataInputStream(in);

int length = getLengthFromStream(stdin);

stdin.reset();

byte[] buffer = getBytesFromStream(stdin);

String sub=null;

String namespace = " xmlns:ns0=\"http://www.opentrans.org/XMLSchema/1.0\" version=\"1.0\">";

String ns_prefix ="<ns0:";

String ns_end_prefix ="</ns0:";

str = str.replaceAll(namespace,">");

str = str.replaceAll(ns_prefix,"<");

str = str.replaceAll(ns_end_prefix,"</");

out.write(str.getBytes());

out.close();

} catch (IOException e) {

}

}

public int getLengthFromStream(InputStream is ) throws

IOException{

int i = 0;

int length = 0;

try{

while ((i = is.read())> 0){

length ++;

}

}catch (ArrayIndexOutOfBoundsException e) {

e.printStackTrace();

}

return length;

}

public byte[] getBytesFromStream(InputStream is) throws

IOException {

// Create the byte array to hold the data

byte[] bytes = new byte[getLengthFromStream(is)];

is.reset();

// Read in the bytes

int offset = 0;

int tmp = 0;

while (offset < bytes.length

&& (is.read(bytes, offset, offset +

(tmp = is.available()))) > 0) {

offset += tmp;

}

// Ensure all the bytes have been read in

if (offset < bytes.length) {

throw new IOException("Could not completely read Inputstream");

}

// Close the input stream and return bytes

is.close();

return bytes;

}

public void setParameter (Map param) {

this.param = param;

if (param == null) {

this.param = new HashMap();

}

}

}

Compile the code ,create a zip file out of .class file,import it as an imported archive and use in your interface mapping.

I have tested this code for the example you gave and it returns:

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

<ORDERRESPONSE>

<ORDERRESPONSE_HEADER>

<ORDERRESPONSE_INFO>

<ORDER_ID>xxxxx<ORDER_ID>

Let us know if it works.

Thanks.

Regards,

Shweta

peter_wallner2
Active Contributor
0 Kudos

Thank you very much Shweta!

I will try the mapping you posted but it might take a week or two because we are in the middle of the system migration and there is a lot to do otherwise.

I will definitely let you know though if it worked!

Thank you again and best regards,

Peter

peter_wallner2
Active Contributor
0 Kudos

Dear Shweta,

I finally had time to try the mapping. I compiled it with Eclipse and JDK 1.4. Before though I had to change 3 things in the code because Eclipse showed errors::

1) I had to declare the variable "str" in the beginning of the code

 String str; 

2) I had to adapt this line:

str = str.replaceAll(ns_prefix,"</");

3) I had to adapt this line:

byte[] bytes = new byte[getLengthFromStream(is)];

I exported the JAR file and imported it into PI.

I tested it in the interface mapping but I got the error:


java.lang.NullPointerException; 
while trying to invoke the method java.lang.String.replaceAll(java.lang.String, java.lang.String)
of an object loaded from field javaPackage.DeleteXMLPrefixes.str of 
an object loaded from local variable 'this'.

Do you know what could be wrong? I don't really know how to test the mapping in Eclipse - I might be able to solve it myself then?

Thank you again!

Best regards, Peter

shweta_walaskar2
Contributor
0 Kudos

Hello Peter,

Do you create this java mapping as a standalone class,from the error message,it seems that you have created it in a package

javaPackage

If this is the case,please add following line as a first line of the code,i.e. before 'import java.io.*;' statement:

package javaPackage;

Let me know if it helps.

It is difficult to test this in Eclipse as a standalone program as there is no main method,you need to create another class with main method which should call execute method of this class.

Regards,

Shweta

peter_wallner2
Active Contributor
0 Kudos

Hello Shweta,

Thank you for your response. I did that and when I import the JAR file into the archive in PI it tells me

Name: DeleteXMLPrefixes.class

Path: javaPackage/

Maybe I should try to generate the JAR file without any package name. I have to look how I do that in Eclipse because everything is always in a project/package.

I will let you know what happened - best regards and thank you!

Peter

Former Member
0 Kudos

Hi,

I think providing the points is also another way of saying best regards and thank you!

Babu

shweta_walaskar2
Contributor
0 Kudos

Hello Peter,

Yes,and if you have a problem in creating JAR file,I can create it without package and if there is a possibility ,will attach it here else send it to you,so that you can directly import it in XI

Regards,

Shweta

peter_wallner2
Active Contributor
0 Kudos

Hello Shweta,

I found out what the problem is with the mapping. The method "StreamTransformation" is deprecated in PI 7.1. I have to use

"...extends AbstractTransformation..."

The second example in this blog Link:/people/stefan.grube/blog/2006/10/23/testing-and-debugging-java-mapping-in-developer-studio

I am already trying to adapt the mapping and testing it in Eclipse...

Best regards,

Peter

Edited by: Peter Wallner on Apr 26, 2010 8:19 AM

peter_wallner2
Active Contributor
0 Kudos

Hello Shweta,

I did my "homework" and came up with a java-mapping for PI 7.1. I found the code in the forum and adapted it a little and also added a main method to test it in Eclipse:


public class Test_Neu extends AbstractTransformation {
	public void execute(InputStream in, OutputStream out)
			throws StreamTransformationException {
		// Add your code here
		String inData = convertStreamToString(in);
		String outData = inData.replaceAll("ns0:","");

		try {
			// The JAVA mapping output payload is returned using the
			// TransformationOutput class
			// arg1.getOutputPayload().getOutputStream()
			out.write(outData.getBytes());
		} catch (Exception exception1) {
				}

	}

	//to test the mapping as a standalone code - main method! delete for PI!
	 public static void main(String[] args) {
		 try {
			InputStream in = new FileInputStream(new File("D:/PETER/in.xml"));
			OutputStream out = new FileOutputStream(new File("D:/PETER/out.xml"));
			Test_Neu myMapping = new Test_Neu(); myMapping.execute(in, out); }
		 catch (Exception e) { e.printStackTrace(); 
		 }
	  }
	 

	public void transform(TransformationInput arg0, TransformationOutput arg1)
			throws StreamTransformationException {
		getTrace().addInfo("JAVA Mapping Called");
		this.execute(arg0.getInputPayload().getInputStream(), arg1
				.getOutputPayload().getOutputStream());
	}

	public String convertStreamToString(InputStream in) {
		StringBuffer sb = new StringBuffer();
		try {
			InputStreamReader isr = new InputStreamReader(in);
			Reader reader = new BufferedReader(isr);
			int ch;
			while ((ch = in.read()) > -1) {
				sb.append((char) ch);
			}
			reader.close();
		} catch (Exception exception) {
		}
		return sb.toString();
	}
}

with

String outData = inData.replaceAll("ns0:","");

I am getting rid of the "ns0:".

But now I also need to add a namespace to my root element and replace some other strings.

Could you help me how I delete/replace multiple elements/strings? Because with .replaceAll for example I can only replace one string at a time.

Thank you again for your help!

Best regards,

Peter

peter_wallner2
Active Contributor
0 Kudos

Hello Shweta,

I found a solution now to my last problem:


String inData = convertStreamToString(in);
		String outdata1 = inData.replaceFirst("<DISPATCHNOTIFICATION version=\"2.1\">",
		"<DISPATCHNOTIFICATION version=\"2.1\" xmlns=\"http://www.opentrans.org/XMLSchema/2.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:bmecat=\"http://www.bmecat.org/bmecat/2005\" xmlns:xmime=\"http://www.w3.org/2005/05/xmlmime\">");
		String outData2 = outdata1.replaceAll("xmlns:ns0=\"http://www.bmecat.org/bmecat/2005\"","");
		String outData3 = outData2.replaceAll("ns0:","bmecat:");
		
		try {
			// The JAVA mapping output payload is returned using the
			// TransformationOutput class
			// arg1.getOutputPayload().getOutputStream()
			out.write(outData3.getBytes());
		} catch (Exception exception1) {
				}

Maybe there would be a more elegant way to do it!? But it works in Eclipse and I just have to test it in PI 7.1 now.

I will let you know about it!

Best regards,

Peter

shweta_walaskar2
Contributor
0 Kudos

Hello Peter,

Sorry for the late reply.Its great to see the adapted code,I work on 3.0,so wasn't aware of this.

Regarding adding/removing multiple namespaces,you can still use .replaceAll function.

For example:

Your root element is : <ns0:ORDERRESPONSE

After this statement: String outData = inData.replaceAll("ns0:","");

Your root element is: <ORDERRESPONSE

You need to add a namespace 'root' to make it <root:ORDERRESPONSE

For doing this,you can add :

String outData = inData.replaceAll("<ORDERRESPONSE","<root:ORDERRESPONSE"); after statement:

String outData = inData.replaceAll("ns0:","");

Same statements can be added further to delete namespaces .

You just need to think about the XML created out of previous segment and can apply a replaceAll statement to update this XML .

Please let me know if this helps you.

Regards,

Shweta

peter_wallner2
Active Contributor
0 Kudos

Thank you Shweta for your help again! I adapted it with the recommendation you made.

The java mappingt also works in PI 7.1 now!!

Best regards,

Peter

Former Member
0 Kudos

Just one additional hint. When playing around with Stream-to-String conversions, take care that you use the right encoding when converting the byte stream into a character stream, in your case when using new InputStreamReader(...).

Otherwise, you will create garbage for special characters. From your description I assume the input stream is UTF-8. But when you do not pass an encoding to the InputStreamReader constructor, the default charset on the PI-server will be taken, and this probably won't be UTF-8

CSY

peter_wallner2
Active Contributor
0 Kudos

Thank you Christian for your additional input.

Would this line be correct then:

out.write(outData3.getBytes("UTF-8"));

Best regards,

Peter

Former Member
0 Kudos

No, the error is before, when you create the InputStreamReader.

getBytes() always returns the bytes of a string, but as the string is already potentially wrong, that is too late.

Within your convertStreamToString method, please change from

InputStreamReader isr = new InputStreamReader(in);

to

InputStreamReader isr = new InputStreamReader(in, "UTF-8");

see http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStreamReader.html for more details on this class.

CSY

peter_wallner2
Active Contributor
0 Kudos

Thank you Christian,

I see! I changed my code according to your code snippet. Thank also for the link to the documentation!

Best regards,

Peter

Answers (2)

Answers (2)

Former Member
0 Kudos

Hey Guys,

I am having issues with using anonymizer bean. Below is the data with namespaces I would like to strip:

________________________________________________________________________________________________

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

- <ns1:ExecuteXMLRequest xmlns:ns1="http://decisionintellect.com/inteport/">

- <ns1:_sRequestXML>

- <ns1:inteflow>

- <ns1:request cd_type="submit">

- <ns1:user>

<ns1:id_oper>INTERFACE</ns1:id_oper>

<ns1:tx_password>password</ns1:tx_password>

</ns1:user>

<ns1:cd_service>SUBMIT</ns1:cd_service>

<ns1:cd_product>IFE</ns1:cd_product>

<ns1:id_channel>INTEGATE</ns1:id_channel>

<ns1:id_company>OFFICEWORKS</ns1:id_company>

<ns1:id_merchant>OFFICEWORKS</ns1:id_merchant>

<ns1:cd_country>61</ns1:cd_country>

<ns1:cd_security>IFE</ns1:cd_security>

</ns1:request>

- <ns1:application_details>

<ns1:id_product_credit>COMMERCIAL DECISIONING</ns1:id_product_credit>

<ns1:id_reference_internal>[APP-ID]</ns1:id_reference_internal>

<ns1:id_reference_external>[customer ref here]</ns1:id_reference_external>

<ns1:id_merchant_submit>OFFICEWORKS</ns1:id_merchant_submit>

</ns1:application_details>

- <ns1:service>

- <ns1:application>

- <ns1:finance>

<ns1:cd_type_finance>TERMS 30</ns1:cd_type_finance>

<ns1:am_finance>4000</ns1:am_finance>

</ns1:finance>

- <ns1:applicant cd_type="Principal" fg_new="0">

- <ns1:company>

<ns1:cd_type_entity>SLTR</ns1:cd_type_entity>

<ns1:tx_company_abn>41552470947</ns1:tx_company_abn>

<ns1:nm_company_legal>WESTON, DEAN</ns1:nm_company_legal>

<ns1:cd_type_industry>OTHER</ns1:cd_type_industry>

<ns1:no_directors>1</ns1:no_directors>

<ns1:no_employees>1</ns1:no_employees>

</ns1:company>

</ns1:applicant>

- <ns1:applicant cd_type="CoBorrower1" fg_new="0">

- <ns1:individual>

<ns1:cd_title>Mr</ns1:cd_title>

<ns1:nm_firstname>Dean</ns1:nm_firstname>

<ns1:nm_surname>Weston</ns1:nm_surname>

<ns1:dt_dob>1968-12-29</ns1:dt_dob>

<ns1:cd_gender>M</ns1:cd_gender>

<ns1:fg_act_privacy>1</ns1:fg_act_privacy>

<ns1:cd_residence>61</ns1:cd_residence>

</ns1:individual>

- <ns1:address>

- <ns1:current_address>

<ns1:tx_no_unit>1</ns1:tx_no_unit>

<ns1:tx_no_street>1</ns1:tx_no_street>

<ns1:nm_street>Test</ns1:nm_street>

<ns1:cd_type_street>Rd</ns1:cd_type_street>

<ns1:nm_suburb>Testerville</ns1:nm_suburb>

<ns1:cd_state>VIC</ns1:cd_state>

<ns1:cd_postcode>3000</ns1:cd_postcode>

<ns1:ct_address_at_time>21</ns1:ct_address_at_time>

<ns1:cd_country>61</ns1:cd_country>

</ns1:current_address>

</ns1:address>

- <ns1:address>

- <ns1:previous_address>

<ns1:tx_no_street>2</ns1:tx_no_street>

<ns1:nm_street>Wayback</ns1:nm_street>

<ns1:cd_type_street>Crs</ns1:cd_type_street>

<ns1:nm_suburb>Sydney</ns1:nm_suburb>

<ns1:cd_state>NSW</ns1:cd_state>

<ns1:cd_postcode>2000</ns1:cd_postcode>

<ns1:ct_address_at_time>117</ns1:ct_address_at_time>

<ns1:cd_country>61</ns1:cd_country>

</ns1:previous_address>

</ns1:address>

- <ns1:identification>

- <ns1:drivers_license>

<ns1:id_number>123456789</ns1:id_number>

<ns1:fg_verified>1</ns1:fg_verified>

</ns1:drivers_license>

</ns1:identification>

- <ns1:contact>

- <ns1:home_phone>

<ns1:tx_area>03</ns1:tx_area>

<ns1:tx_number>99998888</ns1:tx_number>

</ns1:home_phone>

</ns1:contact>

- <ns1:contact>

- <ns1:work_phone>

<ns1:tx_area>03</ns1:tx_area>

<ns1:tx_number>88887777</ns1:tx_number>

</ns1:work_phone>

</ns1:contact>

- <ns1:contact>

- <ns1:other_phone>

<ns1:tx_number>0400123123</ns1:tx_number>

<ns1:cd_type>Mobile</ns1:cd_type>

</ns1:other_phone>

</ns1:contact>

- <ns1:contact>

- <ns1:other_phone>

<ns1:tx_area>03</ns1:tx_area>

<ns1:tx_number>77776666</ns1:tx_number>

<ns1:cd_type>Other</ns1:cd_type>

</ns1:other_phone>

</ns1:contact>

</ns1:applicant>

</ns1:application>

</ns1:service>

</ns1:inteflow>

</ns1:_sRequestXML>

</ns1:ExecuteXMLRequest>

______________________________________________________________________________________________

Anonymizer bean is stripping the namespace prefix ns1 but it is also stripping the address http://decisionintellect.com/inteport/.

Below is my anonymizer bean config in PI:

Processing seq:

1 AF_Modules/XMLAnonymizerBean local enterprise bean 0

Module config:

0 anonymizer.acceptNamespaces http://decisionintellect.com/inteport ''

0 anonymizer.quote '

Can someone please suggest how to keep the http://decisionintellect.com/inteport and remove rest of namespace prefixes

shweta_walaskar2
Contributor
0 Kudos

Hello Peter,

Do you have an option to go for java mapping?

Regards,

Shweta

peter_wallner2
Active Contributor
0 Kudos

Hello Shweta,

The problem is that I can not transport anything for the next 2 weeks because currently we are also updating our system to PI 7.1.

I was already thinking of a workaround by introducing another internal partner that receives the file via FTP and then sends it via X.400 - that should also work but only as a temporary solution.

But for later, yes I could try a java mapping - would you have an example that I can use?

Thank you for your help,

Peter