cancel
Showing results for 
Search instead for 
Did you mean: 

how to display & inside xml

Former Member
0 Kudos

Hi experts,

Im folllowing this blog by felin on xl to xml..

im getting an error every time the cell has &...eveything else is fine

pls let em know

Accepted Solutions (1)

Accepted Solutions (1)

Shabarish_Nair
Active Contributor
0 Kudos

Answers (2)

Answers (2)

Former Member
0 Kudos

sample code


 protected static void copySource(InputStream in, OutputStream out)
	throws IOException {

	int c;
	while ((c = in.read()) != -1) {
	  switch (c){       
		 case '&': out.write("&".getBytes()); break;    
		 case '\"': out.write(""".getBytes()); break;
		 case '\'': out.write("'".getBytes()); break;
		 case '<': out.write("&lt;".getBytes()); break;
		 case '>': out.write("&gt;".getBytes()); break;
		 default: out.write(c);
	   }

modify the above code for module development accordingly

I suggest you to create a method with one arg inputstream and output another inputstream check condition inside the method by reading character by character from the input stream and replace the special characters(as given above) and copy to return inputstream.

former_member187339
Active Contributor
0 Kudos

Hi,

The blog from felix was intended to show that file adapter can also read excel file. But it still have all these problem (like the & issue). I will suggest you to yse this piece of code in you module so that the junk characters are replaced by SPACE


public String check(String node){
		node = node.replaceAll(" ","");
		node = node.replaceAll("[^a-zA-Z 0-9]+",""); 
		return node;
	}

Regards

suraj

Former Member
0 Kudos

Hi Guys,

My module code has

xmldata ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"+ "<ns0:""MT_Test"" ""xmlns:ns0=\"""urn:test:excelfiles"+"\"> \n ";

int a = w.getSheet(0).getRows();

int b = w.getSheet(0).getColumns();

for(int j=1;j<a;j++)

{

for(int i=0;i<b;i++)

{

xmldata = xmldata "\n" "<"w.getSheet(0).getCell(i,0).getContents().toString()">"w.getSheet(0).getCell(i,j).getContents().toString()"</"w.getSheet(0).getCell(i,0).getContents().toString()">"+ "\n";

}

}

xmldata = xmldata" </ns0:""MT_Test"+">";

Now, If cell (2,2) has &... how do i adjust the code?

cos its a string right?

former_member187339
Active Contributor
0 Kudos

Hi

Edit your code like this;


xmldata ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"+ "<ns0:""MT_Test"" ""xmlns:ns0=\"""urn:test:excelfiles"+"\"> \n ";
int a = w.getSheet(0).getRows();
int b = w.getSheet(0).getColumns();
for(int j=1;j<a;j++) {
   for(int i=0;i<b;i++) {
*String Content = check (w.getSheet(0).getCell(i,j).getContents().toString());*
xmldata = xmldata "\n" "<"w.getSheet(0).getCell(i,0).getContents().toString()">"Content"</">"+ "\n";
}
}
xmldata = xmldata+" </ns0:""MT_Test"">";

public String check(String node){
		node = node.replaceAll(" ","");
		node = node.replaceAll("[^a-zA-Z 0-9]+",""); 
		return node;
	}

Be sure that this code will replace & from the input. If you want to change & to &amp; edit this section

String Content = check (w.getSheet(0).getCell(i,j).getContents().toString()); with the appropriate methods (as specified by other users)

Regards

Suraj

Former Member
0 Kudos

Thnks suraj..

but one more info pls.. I dint understand the need of replaceAll("[^a-zA-Z 0-9]+",""); ?

consdering just & can i do node.nodereplaceaAll("&","&amp;") ???

former_member187339
Active Contributor
0 Kudos

Hi,

>>.. I dint understand the need of replaceAll("^a-zA-Z 0-9+","")

This replaceall will replace all special characters from node.

Permissible characters are : * to z and A to Z and 0 to 9*

All special characters like $#%%& etc will be replaced by (SPACE) ""

Regards

Suraj

Former Member
0 Kudos

thank u so much... and for & i put replace with "&AMP;" is it?

Edited by: Ravindra Teja on Sep 15, 2009 3:43 PM

Edited by: Ravindra Teja on Sep 15, 2009 3:44 PM

former_member187339
Active Contributor
0 Kudos

Hi,

>>node.nodereplaceaAll("&","&")

I guess you meant this


node = node.replaceAll("&","&amp;");

This will replace all & with

 &amp;

Regards

Suraj