yeyerl 2012-11-21 23:10 采纳率: 50%
浏览 237
已采纳

java用dom更新xml的问题,怎么在子节点下添加节点?

有原始xml如下:

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <students>
   <student>
     <name sn="03" sn2="0322"/>
   </student>
 </students>

 我想要得到修改后的结果为:

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <students>
   <student>
     <name sn="03" sn2="0322"/>
     <name sn="04" sn2="0422"/>
   </student>
 </students>

 我的代码为:

 

public static void main(String[] args) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse("D:/students.xml");
            Element eltName = doc.createElement("name");
             
            Attr attr = doc.createAttribute("sn");
            attr.setValue("04");
            Attr attr2 = doc.createAttribute("sn2");
            attr2.setValue("0422");
             
            eltName.setAttributeNode(attr);
            eltName.setAttributeNode(attr2);
 
            Element eltRoot=doc.getDocumentElement();
            eltRoot.appendChild(eltName);
            doc2XmlFile(doc, "D:/students.xml");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
/**
     * 将Document对象修改后写入到xml里面
     * @param document Document对象
     * @param filename xml文件路径
     * @return
     */
    public boolean doc2XmlFile(Document document, String filename) {
        boolean flag = true;
        try {
            /** 将document中的内容写入文件中 */
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            /** 编码 */
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(new File(filename));
            transformer.transform(source, result);
        } catch (Exception ex) {
            flag = false;
            System.out.println("更新" + filename + "出错:" + ex);
            log.error("更新" + filename + "出错:" + ex);
            ex.printStackTrace();
        }
        return flag;
    }

 这样得到的结果为:

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <students>
    <student>
      <name sn="03" sn2="0322"/>
    </student>
    <name sn="04" sn2="0422"/>
  </students>

 弄了一天了也没能把添加的name节点放到student节点下面,请教各位大侠了!

 

 

 

 

  • 写回答

3条回答 默认 最新

  • blogzhoubo 2012-11-22 09:16
    关注

    还有一种更好的方法,不需要修改xml文件,

    [code="js"]Element eltRoot=doc.getDocumentElement();

    eltRoot.appendChild(eltName);[/code]

    改成:

    [code="js"]Node stNode = doc.getElementsByTagName("student").item(0);
    stNode.appendChild(eltName);[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格