File fileCfg = new File("c:\Cfg.xml");
    Document cfgDocument;
    XMLOutputter  outter = new XMLOutputter("  ", true, "gb2312");
    try
    {
      if (!fileCfg.exists())
      {
        fileCfg.createNewFile();
        PrintWriter writer = new PrintWriter(new FileOutputStream("c:\Cfg.xml"));
        writer.println(d); // 应该自己添加换行
        writer.flush();
        writer.close();
        cfgDocument = builder.build(fileCfg);
        outter.output(cfgDocument, new FileOutputStream(fileCfg));
      }
      cfgDocument = builder.build(fileCfg);
    }
    catch (Exception ex)
    {    }

解决方案 »

  1.   

    不知道你用什么API,若用JDOM,则SAXBuilder bld=new SAXBuilder();
    Document doc = bld.build(new ByteArrayInputStream(d.getBytes()));
      

  2.   

    netkid(我是个程序员?) :
    那个ByteArrayInputStream()函数,在那个包里面呀?
      

  3.   

    还有我如何对这个Document文档进行遍历那
    如果我用递归的方法,其中用到函数getDocumentElement
    但是在 org.jdom.Document没有这个函数!
    谢谢了!
      

  4.   

    查找所有MyChildNode节点: public static ArrayList getUserList()
      {
        try
        {
          ArrayList list = new ArrayList();
          Iterator iterator = null;
          iterator =
            cfgDocument.getRootElement().getChild("MyNode").getChildren("MyChildNode").iterator();
          if (iterator == null)
          {
            return null;
          }
          while (iterator.hasNext())
          {
            Element eUserID = (Element)iterator.next();
            String strID = eUserID.getText();
            list.add(strID);
          }      return list;
        }
        catch (Exception ex)
        {
          return null;
        }
      }
      

  5.   

    java.io.ByteArrayInputStreamElement rootElm = doc.getRootElement();
    visit(rootElm);visit(Element elm){
       System.out.println(elm);
       List childElms = elm.getChildren();
       Iterator it = childElms.iterator();
       while(it.hasNext()){
          visit((Element)it.next());
       }
    }
      

  6.   

    建议你使用JDOM,非常简单,易用;可以去它的网站jdom.org去下载一些例子、文档什么的