<?xml version="1.0" encoding="UTF-8"?>
<friends>
<connection uid1="1" uid2="2" /> //表示1号用户连接2号用户
<connection uid1="2" uid2="3" /> //表示2号用户连接3号用户
</friends>
问题1:现在我想增加一个用户连接,比如增加2号用户连接4号用户,也就是要在friends里面增加一条
<connection uid1="2" uid2="4" />语句
问题2:同理我要是想删除一个连接,比如删除1号用户对2号用户的连接,也就是要在friends里面删除
<connection uid1="1" uid2="2" /> 
问题3:同理我要是想修改一个连接,比如修改1号用户对2号用户的连接,把它改成对3号用户的连接,也就是
把<connection uid1="1" uid2="2" /> 改成<connection uid1="1" uid2="3" /> 
谢谢大家!!

解决方案 »

  1.   

    获得节点
    对其进行操作
    具体google
      

  2.   

    看dom4j 的Api有这个方法的
      

  3.   


    Document document = null;
    SAXReader saxReader = new SAXReader();
    FileInputStream file;
    String xmlUrl = "e://test.xml";
    try {
    file = new FileInputStream(xmlUrl);
    InputStreamReader reader = new InputStreamReader(file, "UTF-8");
    document = saxReader.read(reader);
    } catch (FileNotFoundException e) { e.printStackTrace();
    } catch (UnsupportedEncodingException e) { e.printStackTrace();
    } catch (DocumentException e) { e.printStackTrace();
    } // 添加
    Element elt = (Element) document.selectSingleNode("//friends");
    Element elt1 = elt.addElement("connection");
    elt1.setAttributeValue("uid1", "2");
    elt1.setAttributeValue("uid2", "4"); // 修改
    Element elt2 = (Element) document
    .selectSingleNode("//friends/connection[@uid1='2'][@uid2='3']");
    document.remove(elt2);
    elt2.setAttributeValue("uid2", "5");
    // 删除
    Element elt3 = (Element) document
    .selectSingleNode("//friends/connection[@uid1='2'][@uid2='4']");
    elt.remove(elt3); XMLWriter output;
    try {
    output = new XMLWriter(new FileWriter(new File("e://test.xml")));
    output.write(document);
    output.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  4.   

    基于Element Node的操作,去看下相关的API
      

  5.   

    查看API吧,这些方法不是很熟悉
      

  6.   

    自己找个dom4j的列子看不就行啦