I want to save/write the xml that time I cant save/write the special character in XML file using JDOM.
When I am trying to save this type of character it give me ?? masrks instead of character.
I am search on this problem but I cant get the proper anwser so please help me.
In my code I am first read the xml and then save/write that xml with few changes.
this is my cide to write the xml.
try {
String path="D://test//N2019_set1.xml";
File structureXml = new File(path);
SAXBuilder saxb = new SAXBuilder();
Document document = saxb.build(structureXml);
Element rootElement = document.getRootElement();
XMLOutputter xmlOutput = new XMLOutputter();
List qestList = rootElement.getChildren();
for (int i = 0; i < qestList.size(); i++) {
Element quesList = (Element) qestList.get(i);
System.out.println(quesList.getAttributeValue("ans"));
//change ans field
quesList.setAttribute("ans", ""+i);
List qList = quesList.getChildren();
for(int a=0;a< qList.size();a++){
Element ques =(Element) qList.get(a);
if(ques.getAttributeValue("file")!=null){
//read xml
System.out.println(ques.getAttributeValue("file"));
//write xml attribute
System.out.println(ques.setAttribute("file","dasd"+a));
}
if(ques.getName().equalsIgnoreCase("question")){
//read
System.out.println(ques.getTextTrim());
ques.removeContent();
ques.addContent(new CDATA("question"+a));
}
if (ques.getName().equalsIgnoreCase("options")) {
List optList = ques.getChildren();
for (int k = 0; k < optList.size(); k++) {
Element option = (Element) optList.get(k);
if(option.getAttributeValue("file")!=null){
System.out.println(option.getAttributeValue("file"));
//write xml attribute
System.out.println(option.setAttribute("file","sadas"+k));
}
if(option.getName().equalsIgnoreCase("option")){
//read
System.out.println(option.getTextTrim());
option.removeContent();
option.addContent(new CDATA("option"+k));
}
}
}
if(ques.getName().equalsIgnoreCase("explaination")){
//read
System.out.println(ques.getTextTrim());
ques.removeContent();
ques.addContent(new CDATA("explaination"+a));
}
}
}
xmlOutput.output(document, new FileWriter(path));
// System.out.println(xmlOutput.outputString(document));
}catch (JDOMException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
this is my xml file
<?xml version="1.0" encoding ="utf-8" ?>
<mcss>
<quest ans="1">
<question><![CDATA[Write 5x= 3y-1 as linear equation form.]]></question>
<options>
<option><![CDATA[5x-3y+1=0]]></option>
<option><![CDATA[-5x-3y-1=0]]></option>
<option><![CDATA[5x+3y+1=0]]></option>
</options>
<explaination><![CDATA[Recall the linear equation in two variables form.]]></explaination>
</quest>
</mcss>
Your question is unclear, but I suspect this may be the problem - it's at least a problem:
xmlOutput.output(document, new FileWriter(path));
FileWriter
always uses the platform default encoding, whereas your XML document claims to be in UTF-8. Use a FileOutputStream
instead, passing that to the output
method... let the XMLOutputter
itself deal with the encoding.
Additionally, it's not clear why you're using all those CDATA
sections - just text should be fine.
See more on this question at Stackoverflow