【问题标题】:how to read xml file and override the same xml file?如何读取 xml 文件并覆盖相同的 xml 文件?
【发布时间】:2013-11-27 00:39:24
【问题描述】:

我正在尝试覆盖我读取的同一个 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 文件。

【问题讨论】:

  • 你的意思是覆盖? DOM 具有读写功能。那还有什么?
  • 我只想写我读过的那个 xml。我的意思是写不是覆盖器。
  • 是的。在 DOM 解析器中是可能的。
  • 但是如何在读取时设置DOM中的属性值?
  • 使用 setAttribute(" ", " ") 您可以在读取元素的位置添加属性。但你最终使用 transformer.transform(s,r) 函数更新了你的 xml 文件。

标签: java xml dom


【解决方案1】:

在同一个 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); 

【讨论】:

  • 通过使用此代码创建了新的任务元素。不要写入已经存在的元素。
  • 没什么难的。选择你的任务节点。然后 quest.appendChild(document.createTextNode("your content"));。就是这样
  • 在我的代码中,每当我打印元素值时,我该如何更改/编写问题或任何元素?
  • @subash--当这段代码写xml的时候CDATA不能读.y?
  • 你的意思是写完之后??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 2023-02-05
相关资源
最近更新 更多