如何读取xml文件并覆盖相同的xml文件?

时间:2013-11-14 05:32:38

标签: java xml dom

我试图覆盖我读取的同一个xml文件。我想编辑xml文件中的所有字段,如question.correct_ans,所有选项和说明
我的xml文件

<mcss>
    <quest ans="3"> 
        <question file="Set2_2.jpg"><![CDATA[A quadrilateral must be a parallelogram if one pair of opposite sides is _____.]]></question>
        <options>
            <option file="Set2_2.jpg"><![CDATA[parallel only]]></option>
            <option><![CDATA[congruent only]]></option>
            <option><![CDATA[congruent and parallel]]></option>
        </options>
        <explaination><![CDATA[In a parallelogram lengths of opposite sides and measure of opposite angles is same.]]></explaination>
    </quest>
    <!--2-->
    <quest ans="1">     
        <question ><![CDATA[The diagonal of any parallelogram forms ______.]]></question>
        <options>
            <option file="Set2_2.png">><![CDATA[x+5=0]]></option>
            <option file="Set2_2.png"><![CDATA[]]></option>
            <option file="Set2_2.png"><![CDATA[]]></option>
            <option file="Set2_2.png"><![CDATA[]]></option>
        </options>
        <explaination><![CDATA[Diagonal of any parallelogram bisects the area of that parallelogram.]]></explaination>  
    </quest>
</mcss>

这是我的java代码

try {
        File fXmlFile = new File("D://test//N2086_set1.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("quest");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                System.out.println("Correct ans : " + eElement.getAttribute("ans"));
                System.out.println("Question: " + eElement.getElementsByTagName("question").item(0).getTextContent());
                NodeList qList = eElement.getElementsByTagName("question");
                    Node qNode = qList.item(0);
                    if (qNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element qElement = (Element) qNode;
                        if(!qElement.getAttribute("file") .isEmpty()){
                            System.out.println("file name : " + qElement.getAttribute("file"));
                        }                   
                    }
                NodeList cList = eElement.getElementsByTagName("options");
                Node cNode = cList.item(0);
                Element cElement = (Element) cNode;
                NodeList scList = cElement.getElementsByTagName("option");
                for(int temp12 = 0; temp12 < scList.getLength(); temp12++){
                    Node scNode = scList.item(temp12);
                    Element scElement = (Element) scNode;
                    if(!scElement.getAttribute("file") .isEmpty()){
                        System.out.println("Ans attr : "+scElement.getAttribute("file"));
                    }
                    System.out.println("option : "+eElement.getElementsByTagName("option").item(temp12).getTextContent());
                }
                System.out.println("Quest ans : " + eElement.getAttribute("ans"));
                System.out.println("Explaination : " + eElement.getElementsByTagName("explaination").item(0).getTextContent());
            }
        }
        } catch (Exception e) {
        e.printStackTrace();
        }

我实现了阅读代码但我无法获得如何覆盖相同的xml文件。

1 个答案:

答案 0 :(得分:0)

在同一个xml中创建任务标记。只需添加此代码。

    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    Element root = document.getDocumentElement();
    Element quest = document.createElement("quest");
    quest.setAttribute("ans","3");
    Node ques = document.createElement("question");
    quest.appendChild(ques);
    root.appendChild(quest);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(new File(tablePath));
    transformer.transform(source, result);