dom4j递归创建xml 现在有这样N个arrayList(当前节点名称,当前节点内容,父节点名称,是否叶子,是否Attribute);想用dom4j递归生成一个document,有现成的代码最好,没有的话给个伪代码也行,谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public Document createDoc() { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("root"); Element author2 = root.addElement("author").addAttribute("name", "Toby").addAttribute("location", "Germany").addText( "Tobias Rademacher"); Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText( "James Strachan"); return doc; }这部分是核心代码,完整的参考请看转载的一个文章 http://www.java2000.net/viewthread.jsp?tid=5724 public Document createDoc() { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("root"); Element author2 = root.addElement("author").addAttribute("name", "Toby").addAttribute("location", "Germany").addText( "Tobias Rademacher"); Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText( "James Strachan"); return doc; } 请参考IBM的这篇文章http://www-128.ibm.com/developerworks/cn/xml/x-dom4j.htmlimport org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.XMLWriter;import java.io.*;public class XmlDom4J{public void generateDocument(){Document document = DocumentHelper.createDocument(); Element catalogElement = document.addElement("catalog"); catalogElement.addComment("An XML Catalog"); catalogElement.addProcessingInstruction("target","text"); Element journalElement = catalogElement.addElement("journal"); journalElement.addAttribute("title", "XML Zone"); journalElement.addAttribute("publisher", "IBM developerWorks"); Element articleElement=journalElement.addElement("article"); articleElement.addAttribute("level", "Intermediate"); articleElement.addAttribute("date", "December-2001"); Element titleElement=articleElement.addElement("title"); titleElement.setText("Java configuration with XML Schema"); Element authorElement=articleElement.addElement("author"); Element firstNameElement=authorElement.addElement("firstname"); firstNameElement.setText("Marcello"); Element lastNameElement=authorElement.addElement("lastname"); lastNameElement.setText("Vitaletti"); document.addDocType("catalog", null,"file://c:/Dtds/catalog.dtd"); try{ XMLWriter output = new XMLWriter( new FileWriter( new File("c:/catalog/catalog.xml") )); output.write( document ); output.close(); } catch(IOException e){System.out.println(e.getMessage());}}public static void main(String[] argv){XmlDom4J dom4j=new XmlDom4J();dom4j.generateDocument();}} lz 凌晨 2 点发帖。。 网上是例子基本都差不多,给lz找了几篇文章,不过程序基本都是差不多,但是解析的都很清楚,希望lz有时间可以认真读一读,写的都很不错。http://www.ibm.com/developerworks/cn/xml/x-dom4j.htmlhttp://blog.csdn.net/cuiweibing/archive/2007/08/14/1743235.aspxhttp://blog.iyi.cn/tech/2005/10/dom4jxml.html 递归也可以采用Iterator作为枚举手段,但文档中提供了另外的做法 public void treeWalk() { treeWalk(getRootElement()); } public void treeWalk(Element element) { for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node instanceof Element) { treeWalk((Element) node); } else { // do something.... } }} 下面这个网页里面有很详细的:http://www.blogjava.net/jiangmin/articles/122016.html 各位看看我这程序错在哪里? JAVA面试题解惑系列(一)——类的初始化顺序 着急啊!还是没有什么思路!大家帮帮忙 学java看啥书? 类被finish修饰了,我想要继承的话该怎么做? Socket类getInputStream()有可能返回NULL吗? 求高手帮我解释一下这道题,感激不尽 有哪位好心人,帮我看看这段程序!!一定给分!!!!!! 给分:lifeis(人就是直立行走的驴) 请进 为什么要使用构造方法 救命呀? 为什么在windows上运行没有问题 ,在linux 下就报错呢 ? 如何查找图片并列出
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
Element author2 = root.addElement("author").addAttribute("name", "Toby").addAttribute("location", "Germany").addText(
"Tobias Rademacher");
Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText(
"James Strachan");
return doc;
}这部分是核心代码,
完整的参考请看转载的一个文章
http://www.java2000.net/viewthread.jsp?tid=5724
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("root");
Element author2 = root.addElement("author").addAttribute("name", "Toby").addAttribute("location", "Germany").addText(
"Tobias Rademacher");
Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText(
"James Strachan");
return doc;
}
http://www-128.ibm.com/developerworks/cn/xml/x-dom4j.htmlimport org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;
public class XmlDom4J{
public void generateDocument(){
Document document = DocumentHelper.createDocument();
Element catalogElement = document.addElement("catalog");
catalogElement.addComment("An XML Catalog");
catalogElement.addProcessingInstruction("target","text");
Element journalElement = catalogElement.addElement("journal");
journalElement.addAttribute("title", "XML Zone");
journalElement.addAttribute("publisher", "IBM developerWorks");
Element articleElement=journalElement.addElement("article");
articleElement.addAttribute("level", "Intermediate");
articleElement.addAttribute("date", "December-2001");
Element titleElement=articleElement.addElement("title");
titleElement.setText("Java configuration with XML Schema");
Element authorElement=articleElement.addElement("author");
Element firstNameElement=authorElement.addElement("firstname");
firstNameElement.setText("Marcello");
Element lastNameElement=authorElement.addElement("lastname");
lastNameElement.setText("Vitaletti");
document.addDocType("catalog",
null,"file://c:/Dtds/catalog.dtd");
try{
XMLWriter output = new XMLWriter(
new FileWriter( new File("c:/catalog/catalog.xml") ));
output.write( document );
output.close();
}
catch(IOException e){System.out.println(e.getMessage());}
}
public static void main(String[] argv){
XmlDom4J dom4j=new XmlDom4J();
dom4j.generateDocument();
}}
public void treeWalk() {
treeWalk(getRootElement());
}
public void treeWalk(Element element) {
for (int i = 0, size = element.nodeCount(); i < size; i++) {
Node node = element.node(i);
if (node instanceof Element) {
treeWalk((Element) node);
} else { // do something....
}
}
}
http://www.blogjava.net/jiangmin/articles/122016.html