。。。。 本帖最后由 ptwhite 于 2010-09-21 15:09:34 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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> 我的意思是在原来的XML文件里进行增、删、改操作,并不是新建一个XML文件 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()); } } 这是JAVA自带的W3C 的XML实现 的示例. 我需要的是DOM对XML的操作,并且更新到文件.... System.out.print(3+2);这个语句怎么在控制台上输出32,我怎么可以输出成5呢 求一个正则表达式 各位大神们,这个数据库怎么查询? 关于导出excel oracle数据库的监听问题。。郁闷坏了。在线等啊。。 Dtree中createTree(Tree,1,7); 的问题 学完JSP后,升级到XML呢,还是Struts? 关于weblogic 7的问题,已经困扰我几天了!!!!!!!!!! 求Java ssh后台管理系统 大家在实际项目开发的时候使用spring一般配置事务怎么配置啊 WEB有前途吗? 如何使用OWC11(Office)
{ 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>
{
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());
}
}