cancel
Showing results for 
Search instead for 
Did you mean: 

XML data - charset encoding problem

Sharathmg
Active Contributor
0 Kudos

Hello all,

I am facing an issue on charset encoding. My requirement is to send an XML and read the the output XML to display the output. The output XML is encoded in "ISO-8859-1" and we are retrieving/reading it in "UTF-8". But some special characteres in the output XML are appearing as it is.

Could some one let me know on how to obtain the desired characters.

Code snippet while reading the XML:

BufferedReader inStream = null;

BufferedWriter outStream = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8"));

inStream =

new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));

Thanks & regards,

Sharath

Accepted Solutions (1)

Accepted Solutions (1)

former_member751941
Active Contributor
0 Kudos

Hi Sharath,

To read the XML file use the following. Don’t mention the character set during reading it.I hope it will help you.

XML file(emp.xml)

-


<?xml version="1.0" encoding="ISO-8859-1"?>

<Emp>

<EmpDetails>

<firstname>Sarbari</firstname>

<lastname>Saha</lastname>

</EmpDetails>

<EmpDetails>

<firstname>Tumpa</firstname>

<lastname>Hazra</lastname>

</EmpDetails>

</Emp>

Java File

-


import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

import org.xml.sax.SAXException;

import org.w3c.dom.NamedNodeMap;

class ReadXML

{

public static void main(String args[])

{

try

{

String fileName="emp.xml";

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document doc = docBuilder.parse (fileName);

NodeList nodeList = doc.getChildNodes();

int nodeSize = nodeList.getLength();

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

{

Node node = nodeList.item(i);

Element elm = (Element) node;

NodeList EmpDetailsList=elm.getElementsByTagName("EmpDetails");

int stNodeSize = EmpDetailsList.getLength();

System.out.println("NodeSize = "+stNodeSize );

for(int j=0;j<stNodeSize;j++)

{

Node nodeEmpdtl = EmpDetailsList.item(j);

Element elmDetails = (Element) nodeEmpdtl;

NodeList firstnameList=elmDetails.getElementsByTagName("firstname");

NodeList lastnameList=elmDetails.getElementsByTagName("lastname");

Node fnameNode=firstnameList.item(0);

System.out.print("Node : " + fnameNode.getNodeName());

System.out.println (" Value : "+((Element)fnameNode).getChildNodes().item(0).getNodeValue());

int lastnameNodeSize = lastnameList.getLength();

Node lnameNode=lastnameList.item(0);

System.out.print("Node : " + lnameNode.getNodeName());

System.out.println(" Value : "+((Element)lnameNode).getChildNodes().item(0).getNodeValue());

}

}

}

catch(ParserConfigurationException pce)

{

System.out.println("Inside ParserConfigurationException Exception");

}

catch(SAXException se)

{

System.out.println("Inside SAXException Exception");

}

catch(IOException ioe)

{

System.out.println("Inside IOException Exception");

}

}

}

Regards,

Mithu

Answers (0)