jdom 生成读取xml数据

来源:互联网 发布:网络节点号 编辑:程序博客网 时间:2024/05/18 00:01

刚刚学了jdom生成xml,写了一个简单的程序,请大家多多指点!

package xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class ComputerXML {
 public void BuildComputerXML() throws JDOMException, FileNotFoundException,
   IOException {
  Element root = new Element("root");
  Document doc = new Document(root);
  Element node, name, icon, explain, node1;
  node = new Element("node");
  name = new Element("name");
  icon = new Element("icon");
  explain = new Element("explain");

  node.setAttribute("id", "n1");
  name.setText("我的电脑");
  icon.setText("images/tree_icon_computer.gif");
  explain.setText("显示连接到此计算机的驱动器和硬件");
  node.addContent(name);
  node.addContent(icon);
  node.addContent(explain);
  root.addContent(node);

  node1 = new Element("node");
  node1.setAttribute("id", "2");
  node1.setAttribute("parentid", "n1");

  node1.addContent(new Element("name").setText("硬盘驱动器 (C:)"));
  node1.addContent(new Element("icon")
    .setText("images/tree_icon_driver.gif"));
  node1.addContent(new Element("explain").setText(""));
  root.addContent(node1);

  Element node2 = new Element("node");
  node2.setAttribute("id", "3");
  node2.addContent(new Element("name").setText("网上邻居"));
  node2.addContent(new Element("icon")
    .setText("images/tree_icon_net.gif"));
  node2.addContent(new Element("explain")
    .setText("显示到网站,网络计算机和FTP站点的快捷方式"));
  root.addContent(node2);

  Element node3 = new Element("node");
  node3.setAttribute("id", "4");
  node3.setAttribute("parentid", "n1");
  node3.addContent(new Element("name").setText("硬盘驱动器 (D:)"));
  node3.addContent(new Element("icon")
    .setText("images/tree_icon_driver.gif"));
  node3.addContent(new Element("explain").setText(""));
  root.addContent(node3);

  Element node4 = new Element("node");
  node4.setAttribute("id", "5");
  node4.setAttribute("parentid", "2");
  node4.addContent(new Element("name").setText("Windows"));
  node4.addContent(new Element("icon").setText(""));
  node4.addContent(new Element("explain").setText(""));
  root.addContent(node4);

  Element node5 = new Element("node");
  node5.setAttribute("id", "6");
  node5.setAttribute("parentid", "3");
  node5.addContent(new Element("name").setText("menu6"));
  node5.addContent(new Element("icon").setText(""));
  node5.addContent(new Element("explain").setText(""));
  root.addContent(node5);

  Element node6 = new Element("node");
  node6.setAttribute("id", "7");
  node6.setAttribute("parentid", "3");
  node6.addContent(new Element("name").setText("menu7"));
  node6.addContent(new Element("icon").setText(""));
  node6.addContent(new Element("explain").setText(""));
  root.addContent(node6);

  Element node7 = new Element("node");
  node7.setAttribute("id", "8");
  node7.setAttribute("parentid", "3");
  node7.addContent(new Element("name").setText("menu8"));
  node7.addContent(new Element("icon").setText(""));
  node7.addContent(new Element("explain").setText(""));
  root.addContent(node7);

  Element node8 = new Element("node");
  node8.setAttribute("id", "9");
  node8.setAttribute("parentid", "7");
  node8.addContent(new Element("name").setText("menu7"));
  node8.addContent(new Element("icon").setText(""));
  node8.addContent(new Element("explain").setText(""));
  root.addContent(node8);

  Element node9 = new Element("node");
  node9.setAttribute("id", "10");
  node9.addContent(new Element("name").setText("回收站"));
  node9.addContent(new Element("icon")
    .setText("images/tree_icon_recycler.gif"));
  node9.addContent(new Element("explain").setText("包含您已经删除的文件和文件夹"));
  root.addContent(node9);

  Element node10 = new Element("node");
  node10.setAttribute("id", "11");
  node10.setAttribute("parentid", "5");
  node10.addContent(new Element("name").setText("system32"));
  node10.addContent(new Element("icon").setText(""));
  node10.addContent(new Element("explain").setText(""));
  root.addContent(node10);

  Element node11 = new Element("node");
  node11.setAttribute("id", "12");
  node11.setAttribute("parentid", "11");
  node11.addContent(new Element("name").setText("system.dll"));
  node11
    .addContent(new Element("link")
      .setText("http://www.lapuasi.com"));
  node11.addContent(new Element("target").setText("_blank"));
  root.addContent(node11);

  Element node12 = new Element("node");
  node12.setAttribute("id", "13");
  node12.setAttribute("parentid", "7");
  node12.addContent(new Element("name").setText("menu13"));
  root.addContent(node12);

  Element node13 = new Element("node");
  node13.setAttribute("id", "14");
  node13.setAttribute("parentid", "n1");
  node13.addContent(new Element("name").setText("DVD 驱动器"));
  node13.addContent(new Element("icon")
    .setText("images/tree_icon_cdrom.gif"));
  node13.addContent(new Element("target").setText(""));
  root.addContent(node13);

  Format format = Format.getCompactFormat();
  format.setEncoding("gb2312");
  format.setIndent("    ");
  XMLOutputter xmlOut = new XMLOutputter(format);
  xmlOut.output(doc, new FileOutputStream("computer.xml"));

 }

 public static void main(String[] args) throws FileNotFoundException,
   JDOMException, IOException {
  ComputerXML XML = new ComputerXML();
  XML.BuildComputerXML();
  System.out.println("building...");
  SAXBuilder builder = new SAXBuilder();
  Document doc_read = builder.build("computer.xml");
  Element root = doc_read.getRootElement();
  List list = root.getChildren();
  System.out
    .println("-----------------ComputerInfor--------------------");
  for (int i = 0; i < list.size(); i++) {
   Element e = (Element) list.get(i);
   String str_attribute = e.getAttributeValue("id");
   String str_parentid = e.getAttributeValue("parentid");
   String str_name = e.getChildText("name");
   String str_icon = e.getChildText("icon");
   String str_explain = e.getChildText("explain");
   String str_link = e.getChildText("link");
   String str_target = e.getChildText("target");

   System.out.println("id:" + str_attribute);

   str_parentid = str_parentid == null ? "" : str_parentid;
   System.out.println("parentid:" + str_parentid);
   System.out.println("Name: " + str_name);
   str_icon = str_icon == null ? "" : str_icon;
   System.out.println("Icon: " + str_icon);
   str_explain = str_explain == null ? "" : str_explain;
   System.out.println("explain: " + str_explain);
   str_link = str_link == null ? "" : str_link;
   System.out.println("link:" + str_link);
   str_target = str_target == null ? "" : str_target;
   System.out.println("str_target:" + str_target);

   System.out
     .println("                                                  ");
  }
  System.out
    .println("--------------------------------------------------");
 }

}

原创粉丝点击