随笔-199  评论-203  文章-11  trackbacks-0

1.        如果要将读取的XML文件,再写入另外的一个新XML文件中,首先必须新建一个和要读取相对应的beans类,通过set方法填充数据,get方法获取数据。

2.        在读取XML文件的时候,需要用到ArrayList集合来存储每次从原XML文件里面读取的数据,在写入新的XML文件的时候,也要通过ArrayList集合获取要遍历的次数,同时将数据写入到新的xml文件中

3.        详细代码如下:

public static void main(String[] args) {

              try {

                     String url = "book.xml";

                     ArrayList list = getBookList(url);

                     //写入一个新的xml文件

                     FileWriter fw = new FileWriter("newbook.xml");

                     fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

                    

                     fw.write("\n<books>");

                     for (int i = 0; i < list.size(); i++) {

                            BookBean book = (BookBean)list.get(i);

                            fw.write("\n<book>\n");

                            if(book.getTitle()!=null){

                                   fw.write("<title>");

                                   fw.write(book.getTitle());

                                   fw.write("</title>\n");

                            }

                            if(book.getAuthor()!=null){

                                   fw.write("<author>");

                                   fw.write(book.getAuthor());

                                   fw.write("</author>\n");

                            }

                            if(book.getPrice()!=null){

                                   fw.write("<price>");

                                   fw.write(book.getPrice());

                                   fw.write("</price>\n");

                            }

                            fw.write("</book>\n");

                     }

                     fw.write("</books>");

                     fw.close();

                    

              } catch (Exception e) {

                     System.out.println(e.getMessage());

              }

       }

      

//获取从一个xml文件中读取的数据,并将其保存在ArrayList中

       public static ArrayList getBookList(String url){

              ArrayList list = new ArrayList();

              try{

                     DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

                     Document doc = db.parse(url);

                     NodeList nodeList = doc.getElementsByTagName("book");

                     for (int i = 0; i < nodeList.getLength(); i++) {

                            String title = doc.getElementsByTagName("title").item(i).getFirstChild().getNodeValue();

                            String author = doc.getElementsByTagName("author").item(i).getFirstChild().getNodeValue();

                            String price = doc.getElementsByTagName("price").item(i).getFirstChild().getNodeValue();

                            BookBean book = new BookBean();

                            book.setTitle(title);

                            book.setAuthor(author);

                            book.setPrice(price);

                            list.add(book);

                     }

              }catch(Exception e){

                     System.out.println(e.getMessage());

              }

              return list;

       }

}

如果你想把这个代码看懂的话,我建议你,先把怎样从XML读取的数据的看懂!

posted on 2009-03-16 07:34 Werther 阅读(3072) 评论(1)  编辑  收藏 所属分类: 10.Java

评论:
# re: 在Java中读取写入XML文件(DEMO_004) 2009-03-16 14:22 | 冷面阎罗
写的方法用jdom 或是dom4j不是比这个好么?

  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: