-2

私が読んだ同じxmlファイルを上書きしようとしています.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 ファイルをオーバーライドする方法がわかりません。

4

1 に答える 1