如何在XML中保存/编写特殊字符?

时间:2013-11-25 08:00:52

标签: java xml dom jdom

我想保存/编写xml,那时我不能使用JDOM在XML文件中保存/写入特殊字符。
当我试图保存这种类型的角色时,它会给我? masrks而不是角色。
我正在寻找这个问题,但我不能得到合适的回答,所以请帮帮我 在我的代码中,我首先阅读xml,然后保存/编写xml,只需进行一些更改。 这是我写的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();
        }

这是我的xml文件

<?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>

1 个答案:

答案 0 :(得分:3)

您的问题尚不清楚,但我怀疑这可能是问题 - 至少 问题:

xmlOutput.output(document, new FileWriter(path));

FileWriter始终使用平台默认编码,而您的XML文档声称采用UTF-8格式。请使用FileOutputStream,将其传递给output方法...让XMLOutputter本身处理编码。

此外,目前还不清楚为什么要使用所有CDATA部分 - 只是文字应该没问题。