掌中招聘网 » 掌中论坛 » 全部 » 关于树的问题
首页 上一页 下一页 尾页 
 本页主题: 关于树的问题
 


lym880822

积分:60
发表主题:56




 发表于 2009-06-23 17:21 资料
楼主

我的测试类是这样的
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>
请问各位哥哥姐姐我怎么改啊
回复1: up
回复2: 谢谢,帮我一下把
回复3: 帮你顶

随便向大家推荐一个IT技术方面的搜索引擎 http://www.zhihuimen.com
回复4: 你要改成什么效果呢?
回复5: 我想让它是这样的啊
<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>
回复6: 不懂,帮顶
回复7: 谁有时间帮我一下呗
回复8: 主要是没测试的代码,你按照这个样子生成不了吗?
回复9: 你得弄清楚几个对象:
Root 根目录
Element 元素
Atrribute 属性
Text 值
对照你想生成的样子,一层一层的添加!

回复10: 我是这样写的,。
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>
回复11: 这个主要是你往root里添加元素的时候没添加正确,你可以添加一个看一下啊。把XML输出到控制台

首页 上一页 下一页 尾页 
 各地招聘导航
·北京 ·天津 ·上海
·西安 ·东莞 ·广州
·深圳 ·南京 ·杭州
·济南 ·南昌 ·兰州
·合肥 ·长沙 ·武汉
·成都 ·重庆 ·太原
·福州 ·沈阳 ·南宁
·海口 ·贵阳 ·郑州
·银川 ·西宁 ·昆明
·拉萨 ·长春
·石家庄  ·乌鲁木齐
·哈尔滨  ·呼和浩特
 各地兼职导航
·北京 ·天津 ·上海
·西安 ·东莞 ·广州
·深圳 ·南京 ·杭州
·济南 ·南昌 ·兰州
·合肥 ·长沙 ·武汉
·成都 ·重庆 ·太原
·福州 ·沈阳 ·南宁
·海口 ·贵阳 ·郑州
·银川 ·西宁 ·昆明
·拉萨 ·长春
·石家庄  ·乌鲁木齐
·哈尔滨  ·呼和浩特