-1

I want to write output file in UTF-8 but couldn't find a way. Can anyone please help me to write the xml in UTF-8

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLFile2 {
    private static byte[] contentInBytes;                

    public static void main(String[] args) {

     try {

      File file = new File("C:\\Users\\abc\\Desktop\\Xml_format\\Done\\part3.xml");

      DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = dBuilder.parse(file);
      // System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

      if (doc.hasChildNodes()) {
            printNote(doc.getChildNodes());
      }

    } catch (Exception e) {
       System.out.println(e.getMessage());
    }

  }

  private static void printNote(NodeList nodeList) {

    for (int count = 0; count < nodeList.getLength(); count++) {
       Node tempNode = nodeList.item(count);
       // make sure it's element node.
       if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

         System.out.println("FIELD" +""+ tempNode.getNodeName() + "=" + tempNode.getTextContent());
         if (tempNode.hasAttributes()) {        
             // get attributes names and values

             NamedNodeMap nodeMap = tempNode.getAttributes();
             for (int i = 0; i < nodeMap.getLength(); i++) {        
                 Node node = nodeMap.item(i);
                 System.out.println("attr name : " + node.getNodeName());
                 System.out.println("attr value : " + node.getNodeValue());
             }

         }

         if (tempNode.hasChildNodes()) {        
             // loop again if has child nodes
             printNote(tempNode.getChildNodes());
         }

         System.out.println(tempNode.getNodeName());

         try {
            File file =new File("C:\\Users\\abc\\Desktop\\Xml_format\\Done\\test.txt");
            String data ="#DREFIELD" +""+ tempNode.getNodeName() + "=" + tempNode.getTextContent()+"";
            //if file doesnt exists, then create it
            if(!file.exists()){
                file.createNewFile();
            }

            //true = append file
            FileWriter fw = new FileWriter(file,true);

            BufferedWriter bw = new BufferedWriter(fw);

            bw.write(data);
            bw.close();

            System.out.println("Done");

          } catch(IOException e){
             e.printStackTrace();
          }
       }
     }

   }
}

I am converting XML to other format which is not writing in special character. It shows ?????.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2650241
  • 7
  • 1
  • 3
  • 1
    http://stackoverflow.com/a/9853008/2504224 – geoand Jul 09 '14 at 11:44
  • Seriously, you did not find the question [How to write a UTF-8 file with Java?](http://stackoverflow.com/questions/1001540/how-to-write-a-utf-8-file-with-java) before submitting this question? – Raedwald Jul 09 '14 at 11:58

2 Answers2

0

You can use this sample:

 try {
        File fileDir = new File("c:\\temp\\test.txt");


    Writer out = new BufferedWriter(new OutputStreamWriter(
        new FileOutputStream(fileDir), "UTF8"));

    out.append("data");


    out.flush();
    out.close();

    } 
   catch (UnsupportedEncodingException e) 
   {
    //todo
   } 
   catch (IOException e) 
   {
    //todo
    }
   catch (Exception e)
   {
    //todo
   } 
desertnaut
  • 57,590
  • 26
  • 140
  • 166
swgkg
  • 184
  • 5
  • Thanks it worked but I am trying to read the xml files which has multiple nodes but its not writing the whole output. It is not iterating whole XML. – user2650241 Jul 09 '14 at 12:03
  • What do you want to do? You read from multiple nodes and write into a single file? – swgkg Jul 09 '14 at 12:17
  • I want to read a single xml file which is having multiple nodes and write into a single file but not printing the whole output in a single file. – user2650241 Jul 09 '14 at 12:22
  • Yes.You can use the above code with the threads.Also please check apache commons vfs with the vfsmanager. it can be used to handle multiple file systems. – swgkg Jul 09 '14 at 12:52
0

Use PrintWriter instead.

File file =new File("C:\\Users\\abc\\Desktop\\Xml_format\\Done\\test.txt");

PrintWriter pw = new PrintWriter (file , 'UTF-8');