使用JDOM创建和解析XML文件

    技术2022-05-20  31

    import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;

    import org.jdom.Document;import org.jdom.Element;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;

    /** * 使用JDOM创建XML文件 *  * @author sageparadise */public class WriteOderXml { public static void CreateXml() throws FileNotFoundException, IOException {  // 创建元素  Element orderRoot = new Element("订单");  // 为根元素添加属性  orderRoot.setAttribute("创建人", "admin");  // 文档对象 就是xml文件在内存中存在的形式  Document doc = new Document(orderRoot);  // 创建根元素的第一个子元素  Element addressee = new Element("A收件人");  orderRoot.addContent(addressee);  // 创建第一个子元素的子元素  Element name = new Element("A姓名");  name.setText("马燕飞");  addressee.addContent(name);  Element street = new Element("A街道");  street.setText("王府井46#");  addressee.addContent(street);  Element city = new Element("A城市");  city.setText("北京");  addressee.addContent(city);  Element duchy = new Element("A直辖市");  duchy.setText("北京");  addressee.addContent(duchy);  Element post = new Element("A邮编");  post.setText("1000001");  // 创建根元素的第二个子元素  Element ddate = new Element("订货日期");  ddate.setText("2000-12-1");  orderRoot.addContent(ddate);  // 创建根元素的第三个子元素  Element fdate = new Element("发货日期");  fdate.setText("2000-12-19");  orderRoot.addContent(fdate);  // 创建根元素的第四个子元素  Element remark = new Element("备注");  remark.setText("日常用品系列");  orderRoot.addContent(remark);  // 创建根元素的第五个子元素  Element ware = new Element("货物单");  orderRoot.addContent(ware);  // 创建子元素的子元素  Element goods = new Element("商品");  ware.addContent(goods);  Element gname = new Element("名称");  gname.setText("紫罗兰");  goods.addContent(gname);  Element grade = new Element("等级");  grade.setText("A");  goods.addContent(grade);  Element price = new Element("价格");  price.setText("20");  goods.addContent(price);  Element goods1 = new Element("商品");  ware.addContent(goods1);  Element gname1 = new Element("名称");  gname1.setText("霸王洗发水");  goods1.addContent(gname1);  Element grade1 = new Element("等级");  grade1.setText("A");

      goods1.addContent(grade1);  Element price1 = new Element("价格");  price1.setText("50");  goods1.addContent(price1);

      // 设置显示格式  Format format = Format.getRawFormat();  format.setIndent("    ");  XMLOutputter out = new XMLOutputter(format.setEncoding("UTF-8"));  out.output(doc, new FileOutputStream(new File("D://updir//order.xml"))); }

     public static void main(String[] agrs) throws FileNotFoundException,   IOException {  WriteOderXml order = new WriteOderXml();  order.CreateXml();

     }

    }

     

     

    import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;

    import org.jdom.Document;import org.jdom.Element;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;

    /** * 使用JDOM创建XML文件 *  * @author sageparadise */public class WriteOderXml { public static void CreateXml() throws FileNotFoundException, IOException {  // 创建元素  Element orderRoot = new Element("订单");  // 为根元素添加属性  orderRoot.setAttribute("创建人", "admin");  // 文档对象 就是xml文件在内存中存在的形式  Document doc = new Document(orderRoot);  // 创建根元素的第一个子元素  Element addressee = new Element("A收件人");  orderRoot.addContent(addressee);  // 创建第一个子元素的子元素  Element name = new Element("A姓名");  name.setText("马燕飞");  addressee.addContent(name);  Element street = new Element("A街道");  street.setText("王府井46#");  addressee.addContent(street);  Element city = new Element("A城市");  city.setText("北京");  addressee.addContent(city);  Element duchy = new Element("A直辖市");  duchy.setText("北京");  addressee.addContent(duchy);  Element post = new Element("A邮编");  post.setText("1000001");  // 创建根元素的第二个子元素  Element ddate = new Element("订货日期");  ddate.setText("2000-12-1");  orderRoot.addContent(ddate);  // 创建根元素的第三个子元素  Element fdate = new Element("发货日期");  fdate.setText("2000-12-19");  orderRoot.addContent(fdate);  // 创建根元素的第四个子元素  Element remark = new Element("备注");  remark.setText("日常用品系列");  orderRoot.addContent(remark);  // 创建根元素的第五个子元素  Element ware = new Element("货物单");  orderRoot.addContent(ware);  // 创建子元素的子元素  Element goods = new Element("商品");  ware.addContent(goods);  Element gname = new Element("名称");  gname.setText("紫罗兰");  goods.addContent(gname);  Element grade = new Element("等级");  grade.setText("A");  goods.addContent(grade);  Element price = new Element("价格");  price.setText("20");  goods.addContent(price);  Element goods1 = new Element("商品");  ware.addContent(goods1);  Element gname1 = new Element("名称");  gname1.setText("霸王洗发水");  goods1.addContent(gname1);  Element grade1 = new Element("等级");  grade1.setText("A");

      goods1.addContent(grade1);  Element price1 = new Element("价格");  price1.setText("50");  goods1.addContent(price1);

      // 设置显示格式  Format format = Format.getRawFormat();  format.setIndent("    ");  XMLOutputter out = new XMLOutputter(format.setEncoding("UTF-8"));  out.output(doc, new FileOutputStream(new File("D://updir//order.xml"))); }

     public static void main(String[] agrs) throws FileNotFoundException,   IOException {  WriteOderXml order = new WriteOderXml();  order.CreateXml();

     }

    }

     

     


    最新回复(0)