我的测试类是这样的
Module root = new Module() ;
root.setParent(null) ;
root.setModuleId("1");
root.setModuleName("billing report") ;
root.setLocation("/report");
Module node1 = new Module() ;
node1.setParent(root) ;
node1.setModuleId("11");
node1.setModuleName("traffic report") ;
node1.setLocation("/report/traffic");
Module node2 = new Module() ;
node2.setParent(root) ;
node2.setModuleId("12");
node2.setModuleName("hits report") ;
node2.setLocation("/report/hits"); List<Module> modules = new ArrayList<Module>() ;
modules.add(root);
modules.add(node1);
modules.add(node2);
ToCreateXML generator = new ToCreateXML() ;
try {
Document document = generator.createDoculemt(modules);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(System.out, format) ;
writer.write(document) ;
writer.close();
} catch (DocumentException e) {......
想得到一棵树,
ToCreateXML这个类是这样写的
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("tree");
Element child1 = root.addElement("entity");
Element child2 = child1.addElement("leaf");
Element child3 = child1.addElement("description");
Element child4 = child1.addElement("image");
Element child5 = child1.addElement("imageOpen");
Element e = child1.addElement("entity");
for(int i=0;i<root.nodeCount();i++){
module = li.get(i);
if(module.getParent() == null){
root.appendAttributes(child1);
e.addAttribute("id",""+module.getLocation());
Element e1 = child1.addElement("leaf");
e1.setText("true");
Element e2 = child1.addElement("description");
e2.setText(module.getModuleName());
Element e3 = child1.addElement("image");
e3.setText("../photo/catalog.gif");
Element e4 = child1.addElement("imageOpen");
e4.setText("../photo/catalogOpen.gif");
}else{
child1.addAttribute("id",""+module.getLocation());
child2.setText("true");
child3.setText(module.getModuleName());
child4.setText("../photo/catalog.gif");
child5.setText("../photo/catalogOpen.gif");
child1.appendAttributes(e);
}
}
return doc;
得到的测试结果确实这样的
<tree>
<entity>
<leaf/>
<description/>
<image/>
<imageOpen/>
<entity id="/report"/>
<leaf>true</leaf>
<description>billing report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
</tree>
请问各位哥哥姐姐我怎么改啊
Module root = new Module() ;
root.setParent(null) ;
root.setModuleId("1");
root.setModuleName("billing report") ;
root.setLocation("/report");
Module node1 = new Module() ;
node1.setParent(root) ;
node1.setModuleId("11");
node1.setModuleName("traffic report") ;
node1.setLocation("/report/traffic");
Module node2 = new Module() ;
node2.setParent(root) ;
node2.setModuleId("12");
node2.setModuleName("hits report") ;
node2.setLocation("/report/hits"); List<Module> modules = new ArrayList<Module>() ;
modules.add(root);
modules.add(node1);
modules.add(node2);
ToCreateXML generator = new ToCreateXML() ;
try {
Document document = generator.createDoculemt(modules);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(System.out, format) ;
writer.write(document) ;
writer.close();
} catch (DocumentException e) {......
想得到一棵树,
ToCreateXML这个类是这样写的
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("tree");
Element child1 = root.addElement("entity");
Element child2 = child1.addElement("leaf");
Element child3 = child1.addElement("description");
Element child4 = child1.addElement("image");
Element child5 = child1.addElement("imageOpen");
Element e = child1.addElement("entity");
for(int i=0;i<root.nodeCount();i++){
module = li.get(i);
if(module.getParent() == null){
root.appendAttributes(child1);
e.addAttribute("id",""+module.getLocation());
Element e1 = child1.addElement("leaf");
e1.setText("true");
Element e2 = child1.addElement("description");
e2.setText(module.getModuleName());
Element e3 = child1.addElement("image");
e3.setText("../photo/catalog.gif");
Element e4 = child1.addElement("imageOpen");
e4.setText("../photo/catalogOpen.gif");
}else{
child1.addAttribute("id",""+module.getLocation());
child2.setText("true");
child3.setText(module.getModuleName());
child4.setText("../photo/catalog.gif");
child5.setText("../photo/catalogOpen.gif");
child1.appendAttributes(e);
}
}
return doc;
得到的测试结果确实这样的
<tree>
<entity>
<leaf/>
<description/>
<image/>
<imageOpen/>
<entity id="/report"/>
<leaf>true</leaf>
<description>billing report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
</tree>
请问各位哥哥姐姐我怎么改啊
<tree>
<entity id="/report">
<description>billing report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
<entity id="/report/traffic">
<leaf>true</leaf>
<description>traffic report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
<entity id="/report/hits">
<leaf>true</leaf>
<description>hits report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
</entity>
</tree>
Root 根目录
Element 元素
Atrribute 属性
Text 值
对照你想生成的样子,一层一层的添加!
public Document createDoculemt(List<Module> li)throws DocumentException {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("tree");
Element child1 = root.addElement("entity");
for(int i=0;i<root.nodeCount();i++){
module = li.get(i);
if(module.getParent() == null){
Element e = root.addElement("entity").addAttribute("id",""+module.getLocation());
e.addElement("description").setText(module.getModuleName());
e.addElement("image").setText("../photo/catalog.gif");
e.addElement("imageOpen").setText("../photo/catalogOpen.gif");
e.addElement("contents").appendContent(child1);
}else{
child1.addAttribute("id",""+module.getLocation());
child1.addElement("leaf").setText("true");
child1.addElement("description").setText(module.getModuleName());
child1.addElement("image").setText("../photo/catalog.gif");
child1.addElement("imageOpen").setText("../photo/catalogOpen.gif");
}
}
return doc;
}
测试出来的结果是这样的
<?xml version="1.0" encoding="UTF-8"?><tree>
<entity id="/report/traffic">
<leaf>true</leaf>
<description>traffic report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
<entity id="/report">
<description>billing report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
<contents/>
</entity>
</tree>
但是不我想要的,我想要的是这样的
<?xml version="1.0" encoding="UTF-8"?>
<tree>
<entity ="/report">
<description>billing report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
<contents>
<entity id="/report/traffic">
<leaf>true</leaf>
<description>traffic report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
<entity id="/report/hits">
<leaf>true</leaf>
<description>hits report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
</contents>
</entity>
</tree>
SAXBuilder和XMLOutputter用这两个的时候要引什么包啊
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;public class Test {
public static void main(String[] args) {
Module root = new Module() ;
root.setParent(null) ;
root.setModuleId("1");
root.setModuleName("billing report") ;
root.setLocation("/report"); Module node1 = new Module() ;
node1.setParent(root) ;
node1.setModuleId("11");
node1.setModuleName("traffic report") ;
node1.setLocation("/report/traffic"); Module node2 = new Module() ;
node2.setParent(root) ;
node2.setModuleId("12");
node2.setModuleName("hits report") ;
node2.setLocation("/report/hits"); List <Module> modules = new ArrayList <Module>() ;
modules.add(root);
modules.add(node1);
modules.add(node2); ToCreateXML generator = new ToCreateXML() ;
try {
Document document = generator.createDoculemt(modules);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(System.out, format) ;
writer.write(document) ;
writer.close();
} catch (Exception e) {
}
}
}
/**
* 只在这改了你的代码
*/
class ToCreateXML{
public Document createDoculemt(List <Module> li)throws DocumentException {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("tree");
for(int i=0;i <li.size();i++){
Module module = li.get(i);
if(module.getParent() == null){
Element e = root.addElement("entity").addAttribute("id",""+module.getLocation());
e.addElement("description").setText(module.getModuleName());
e.addElement("image").setText("../photo/catalog.gif");
e.addElement("imageOpen").setText("../photo/catalogOpen.gif");
Element contents = null;
for(int j = 0;j < li.size();j++){
Module child = li.get(j);
if(module.equals(child.getParent())){
if(contents == null){
contents = e.addElement("contents");
}
Element childe = contents.addElement("entity").addAttribute("id",""+child.getLocation());
childe.addElement("leaf").setText("true");
childe.addElement("description").setText(child.getModuleName());
childe.addElement("image").setText("../photo/catalog.gif");
childe.addElement("imageOpen").setText("../photo/catalogOpen.gif");
}
}
}
}
return doc;
}
}
class Module {
private Object parent;
private String moduleId;
private String moduleName;
private String location;
public Object getParent() {
return parent;
}
public void setParent(Object parent) {
this.parent = parent;
}
public String getModuleId() {
return moduleId;
}
public void setModuleId(String moduleId) {
this.moduleId = moduleId;
}
public String getModuleName() {
return moduleName;
}
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}}
测试结果:
<?xml version="1.0" encoding="UTF-8"?><tree>
<entity id="/report">
<description>billing report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
<contents>
<entity id="/report/traffic">
<leaf>true</leaf>
<description>traffic report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
<entity id="/report/hits">
<leaf>true</leaf>
<description>hits report</description>
<image>../photo/catalog.gif</image>
<imageOpen>../photo/catalogOpen.gif</imageOpen>
</entity>
</contents>
</entity>
</tree>