开发者

Strange character result in xml by java code

I generate the XML by reading the text form .txt file. But I got strange character results. I want to see my text in xml same as it is shown in .txt file.

here is my text from .txt file

žena

muškarac

devojčica

dečak

automobil

autobus

drvo

kuća

avion

mačka

pas

cvet

and here is my xml strange result.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
    <string name="0s0">žena</string>
    <string name="1s1">muškarac</string>
    <string name="2s2">devoj�ica</string>
    <string name="3s3">de�ak</string>
    <string name="4s4">automobil</string>
    <string name="5s5">autobus</string>
    <string name="6s6">drvo</string>
    <string name="7s7">kuća</string>
    <string name="8s8">avion</string>
    <string name="9s9">ma�ka</string>  

Here is my code.

          FileInputStream fstream = new FileInputStream("D:/12.txt"); // Path of input text file 
          BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
          String strLine;

          DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

          //root elements
          Document doc = docBuilder.newDocument();
          Element rootElement = doc.createElement("resources");
          doc.appendChild(rootEl开发者_开发问答ement);

          int i = 0;
          String attrName;
          //Read File Line By Line
          while ((strLine = br.readLine()) != null)   {

              attrName = i+"s"+i;
              i++;

              //staff elements
              Element string = doc.createElement("string");
              rootElement.appendChild(string);

              //set attribute to staff element
              Attr attr = doc.createAttribute("name");
              attr.setValue(attrName);
              string.setAttributeNode(attr);
              string.appendChild(doc.createTextNode(strLine));

          }
          //Close the input stream
          in.close();

          //write the content into xml file
          TransformerFactory transformerFactory = TransformerFactory.newInstance();
          Transformer transformer = transformerFactory.newTransformer();
          DOMSource source = new DOMSource(doc);
          StreamResult result =  new StreamResult(new File("D:\\italian.xml")); // Path of output text file
          transformer.transform(source, result);

          System.out.println("Done");        

I studied lot of posts in here but I could not get my desire solution. Just I want to see the same charecters / text in xml file as these are shown in .txt file.


You need to provide the correct Charset/Encoding when using the InputStreamReader. Try this:

... new InputStreamReader(in, "UTF-8");


Try setting the encoding of the inputStreamReader, for example

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

If this doesn't work, try to figure out which encoding your file uses.


You have two problems.

  1. You don't know what encoding your .txt-file is in. You need to read it with the correct encoding, possibly UTF-8. When you just use new InputStreamReader() without specifying an encoding, java will use the platform default encoding, which is equivalent to using a random encoding.

  2. The XML-file is stored with encoding UTF-8 which is the default. If you want to read it in a text editor, you need an editor that supports UTF-8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜