本帖最后由 ptwhite 于 2010-09-21 15:09:34 编辑

解决方案 »

  1.   

    public static void main(String[] args)
    { try
    {
    String uri = "book.xml";
    ArrayList list = getBookList(uri);
    FileWriter fs = new FileWriter("newbook.xml");
    fs.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    fs.write("\n<books>");
    for (int i = 0; i < list.size(); i++)
    {
    Book book = (Book) list.get(i);
    fs.write("\n    <book>\n");
    if (book.getTitle() != null)
    {
    fs.write("<title>");
    fs.write(book.getTitle());
    System.out.println(book.getTitle());
    fs.write("</title>\n");
    }
    if (book.getAuthor() != null)
    {
    fs.write("<author>");
    fs.write(book.getAuthor());
    fs.write("</author>\n");
    }
    if (book.getPrice() != null)
    {
    fs.write("<price>");
    fs.write(book.getPrice());
    fs.write("</price>\n");
    }
    fs.write("    </book>\n");
    }
    fs.write("</books>");
    fs.close();
    } catch (IOException ioe)
    {
    System.out.println(ioe.getMessage());
    } }<?xml version="1.0" encoding="UTF-8"?>
    <books>
    <book>
    <title>面向对象设计</title>
    <author>张三</author>
    <price>50.00</price>
    </book>
    <book>
    <title>Oracle数据库</title>
    <author>李四</author>
    <price>78.00</price>
    </book>
    <book>
    <title>征服AJAX2.0</title>
    <author>王五</author>
    <price>58.00</price>
    </book>
    </books>
      

  2.   

         我的意思是在原来的XML文件里进行增、删、改操作,并不是新建一个XML文件
      

  3.   

    public static void writeDocument(String filename, Document document) throws FileNotFoundException, IOException
    {
    writeDocument(new File(filename), document);
    } public static void writeDocument(File outFile, Document document) throws FileNotFoundException, IOException
    {
    if (null == document) return;
    if (null == outFile) return;
    FileOutputStream fos = new FileOutputStream(outFile);
    writeDocument(fos, document);
    if (fos != null) fos.close();
    } public static void writeDocument(OutputStream os, Document document) throws IOException
    {
    if (null == document) return;
    if (null == os) return;
    synchronized (lockObj)
    {
    OutputFormat format = new OutputFormat(document);
    format.setIndent(4);
    format.setPreserveSpace(false);
    format.setPreserveEmptyAttributes(true);
    format.setLineWidth(Integer.MAX_VALUE);
    XMLSerializer serializer = new XMLSerializer(os, format);
    serializer.asDOMSerializer();
    serializer.serialize(document.getDocumentElement());
    }
    }
      

  4.   

    这是JAVA自带的W3C 的XML实现 的示例.
      

  5.   

       我需要的是DOM对XML的操作,并且更新到文件....