-1

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>
vijayk
  • 2,633
  • 14
  • 38
  • 59
  • 1
    Why are you using CDATA at all? And what's the "special character" here? Your code doesn't mention it... – Jon Skeet Nov 25 '13 at 08:03
  • thanks for reply.. ≠ this type of character if you going to write this then after save it looks like ? mark. – vijayk Nov 25 '13 at 08:16

1 Answers1

3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @vijayk: *Why* do you have to have CDATA in your XML? That sounds like a broken requirement - the non-CDATA version should be semantically equivalent. – Jon Skeet Nov 25 '13 at 08:42
  • by using this xml we are launch the player. To open the xml and all the content of xml in player it requires the CDATA. – vijayk Nov 25 '13 at 09:42
  • @vijayk: This is the first time you've mentioned a "player" - we have no idea what your context is. Still, if it requires CDATA then it's fundamentally broken :( – Jon Skeet Nov 25 '13 at 10:30
  • Can u plz tell me how to remove child element..eg.option.getParentElement().removeChild("option") bt this code remove all options.I just want to remove last option. – vijayk Dec 19 '13 at 07:11
  • @vijayk: That's a completely different question - and should be asked in a different post. – Jon Skeet Dec 19 '13 at 07:18
  • http://stackoverflow.com/questions/20675639/how-to-remove-child-element-from-xml-in-java – vijayk Dec 19 '13 at 07:21