2

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(rootElement);

          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.

2
  • 1
    you need to mark your XML as source code to make them show up. Just select it and push the {} symbol (note that his has already been done for you this time). Commented Jun 1, 2011 at 14:04
  • note that there is no need to create a DataInputStream in this code - you can pass the FileInputStream instance to the InputStreamReader directly. Commented Jun 1, 2011 at 14:04

3 Answers 3

4

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

... new InputStreamReader(in, "UTF-8");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Tim, Problem has been solved by your way.. InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
@Khokhar: That's great. You may want to accept this answer as correct, by clicking the checkmark next to it.
1

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.

Comments

1

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.

1 Comment

Thanks, this problem has been solved by InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); and // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("D:/myfile.xml")); transformer.transform(source, result);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.