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.
{}symbol (note that his has already been done for you this time).DataInputStreamin this code - you can pass theFileInputStreaminstance to theInputStreamReaderdirectly.