不知道,我只知道怎么用jdom读取xml文件,要写一个xml文件就不怎么清楚了呵呵,但是我觉得你这个方法好像不是很好啊,为什么要把有的写入xml文件存取呢,直接插入到数据库不是很直接和方便么?

解决方案 »

  1.   

    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.output.*;
    import java.io.*;public class rw_xml {  public static void main(String args[]) {
        try {
          SAXBuilder sb = new SAXBuilder();
    //创建文档
          Document doc = sb.build(new FileInputStream("E:\\a.xml"));
    //获得这个文档得跟元素
          Element el = doc.getRootElement();
    //获得这个跟元素,的所有子元素
          java.util.List ls = el.getChildren();
    //得到第一个子元素
          Element book = (Element) ls.get(0);//获得这个元素的子元素,(指定)
          Element el2 = book.getChild("listst");
    //输出这个元素的值
          System.out.println(el2.getName());
    //给这个元素的值改个名字
          el2.setText("cute");//再指定元素获得这个值
          Element el3 = book.getChild("list");
          String a = "";
          boolean bool = true;
          XMLOutputter xml = new XMLOutputter(a, bool, "gb2312");
          xml.output(doc, new FileOutputStream("E:\\cute.xml"));
        }
        catch (Exception e) {
          System.out.println(e.getMessage());
        }
      }
    }根据这个,来试试
      

  2.   

    <lists>
      <list>
        <id>1</id>
        <username>冯奎</username>
        <time>01-4-23 18:26:56</time>
        <homepage>http://www.getjob.com.cn</homepage>
        <email>[email protected]</email>
        <context>我的留言</context>
      </list>
    </lists>
    这个如何增加到a.xml中
      

  3.   

    public void EditXML(String filename,String element){
        File fn = new File(filename);
        Document doc = null;
        SAXBuilder sb = new SAXBuilder();
        if(fn.exists()){
          try{
            doc = sb.build(new FileInputStream(filename));
          }catch(Exception e){
            System.out.println(e.getMessage());
          }
          Element root = doc.getRootElement();
          //List list_two = root.getChildren();
          //Element el_two = root.getChild("two");
          Element el_two = root.getChild(element);
          if(root.getContentSize()>0){
            int index = root.indexOf(el_two);
            Element el_three = new Element("three");
            el_three.addContent("three");
            el_three.setAttribute("memo","new add");
            root.addContent(index+1,el_three);
            //
            //设置输出格式
            Format f = Format.getRawFormat();
            f.setIndent(" ");
            f.setEncoding("GBK");
            XMLOutputter output = new XMLOutputter(f);
            try{
              output.output(doc, new FileOutputStream(filename));
            }catch(Exception e){
              System.out.println(e.getMessage());
            }
          }else{
            System.out.println("Not found child element!");
          }
        }else{
          System.out.println("找不到指定的文件!");
        }
      }
      public static void main(String[] args) {
        //修改XML文件
        operateXML1.EditXML("output.xml","one");
      }
    output.xml文件的内容:
    <?xml version="1.0" encoding="GBK"?>
    <root name="root">
     <one>one</one>
     <three memo="new add">three</three>
     <two name="two">two</two>
     <!--This is a test!-->
    </root>
    楼主稍加改造应该能实现楼主的要求!
      

  4.   

    请问楼上的,这只是建立一个,现在由于数据关系,我还要建立(不止一个)类似如
    <?xml version="1.0" encoding="GBK"?>
    <root>
     <one>one</one>
     <two name="two">two</two>
     <!--This is a test!-->
    </root>
    <root>
     <one>three</one>
     <four name="four">four</four>
     <!--This is a test!-->
    </root>
    <root>
     <one>five</one>
     <six name="six">two</six>
     <!--This is a test!-->
    </root>
      

  5.   

    严格来说,xml只允许有一个根元素,你可以在里面加一个唯一的根元素,然后再用Element root = doc.getRootElement();
          List list_two = root.getChildren();不就行了
      

  6.   

    如何在一个已经有的XML文件增加类似的结点信息
    比如前面
    <?xml version="1.0" encoding="GBK"?>
    <root>
     <one>one</one>
     <two name="two">two</two>
     <!--This is a test!-->
    </root>我要在xml文件下面接着增加
    <root>
     <one>three</one>
     <four name="four">four</four>
     <!--This is a test!-->
    </root>
    <root>
     <one>five</one>
     <six name="six">two</six>
     <!--This is a test!-->
    </root>
    等等信息
      

  7.   

    其实我上面的代码已经很清晰了,既然能在一个指定节点下边插入一个新的同级节点,稍加改造一下当然也能满足你的要求了,建议楼主还是先找些jdom的资料来看看!!还有,就是一个xml文件中只能有一个根节点!!