DOM4J生成xml文件

5 篇文章 0 订阅

DOM4J生成xml文件主要步骤:

1. 创建document对象,代表整个XML
2. 创建根节点
3. 向跟节点中添加属性
4. 生成子节点及其内容
5. 设置生成xml的格式
6. 生成xml文件

例子:

DOM4JTest {
    private static List<NewItem> NewItemList = new ArrayList<NewItem>();
    private void createXML(){
        //1、创建document对象,代表整个XML
        Document document = DocumentHelper.createDocument();
        //2、创建根节点rss
        Element  rss= document.addElement("rss");
        //3、向rss节点中添加version属性
        rss.addAttribute("version", "2.0");
        //4、生成子节点及其内容
        Element channel = rss.addElement("channel");
        Element title = channel.addElement("title");
        title.setText("<![CDATA[时政频道]]>");
        Element image = channel.addElement("image");
        Element imageTitle = image.addElement("title");
        imageTitle.setText("<![CDATA[时政频道]]>");
        Element imageLink = image.addElement("link");
        imageLink.setText("http://politics.people.cn");
        Element imageUrl = image.addElement("url");
        imageUrl.setText("http://www.people.cn/img/2014peoplelogo/rmw_logo.gif");
        Element description = channel.addElement("description");
        description.setText("<![CDATA[时政新闻]]");
        Element link = channel.addElement("link");
        link.setText("http://politics.people.cn");
        Element language = channel.addElement("language");
        language.setText("zh-cn");
        Element generator = channel.addElement("generator");
        generator.setText("www.people.cn");
        Element copyright = channel.addElement("copyright");
        copyright.setText("<![CDATA[Copyright 1997-2016 by www.people.com.cn. all rights reserved]]>");
        Element pubDate = channel.addElement("pubDate");
        pubDate.setText("2016-07-07 22:03:00");
        for (NewItem itemNew : NewItemList) {
            Element item = channel.addElement("item");
            Element itemTitle = item.addElement("title");
            itemTitle.setText(itemNew.getTitle());
            Element itemLink = item.addElement("link");
            itemLink.setText(itemNew.getLink());
            Element itemPubDate = item.addElement("pubDate");
            itemPubDate.setText(itemNew.getPubDate());
            Element itemAuthor = item.addElement("author");
            itemAuthor.setText(itemNew.getAuthor());
            Element itemGuid = item.addElement("guid");
            itemGuid.setText(itemNew.getGuid());
            Element itemDescription = item.addElement("description");
            itemDescription.setText(itemNew.getDescription());
        }
        //5、设置生成xml的格式
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        //6、生成xml文件
        File file = new File("rssnews.xml");
        XMLWriter writer;
        try {
            writer = new XMLWriter(new FileOutputStream(file),format);
            //设置是否转义,默认为true,代表 
            writer.setEscapeText(false);
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void createNewsItem() {
        NewItem item = new NewItem();
        item.setTitle("<![CDATA[湖北入梅以来洪涝、风雹等灾害已造成直接损失270亿]]>");
        item.setLink("http://politics.people.com.cn/n1/2016/0707/c1001-28534758.html");
        item.setPubDate("2016-07-07");
        item.setAuthor("人民网");
        item.setGuid("http://politics.people.com.cn/n1/2016/0707/c1001-28534758.html");
        item.setDescription("<![CDATA[&amp;$ 人民网北京7月7日电&nbsp;湖北省人民政府新闻办公室官方微博今日发布消息称,截至7月7日16时统计,6月18日入梅以来洪涝、风雹等自然灾害已造成全省17个市(州、直管市、林区)92个县(市、区)1781.26万人次受灾,死亡69人失踪16人,紧急转移安置73.2万人次,需紧急生活救助47.5万人次;  因灾倒塌房屋2.9万间,严重损坏房屋3.5万间,一般损坏3.8万间;  农作物受灾面积1558.3千公顷,其中绝收359.5千公顷;  直接经济损失270.7亿元。   &amp;$]]>");
        NewItemList.add(item);

        NewItem item2 = new NewItem();
        item2.setTitle("<![CDATA[公立医院特需禁超全部服务10%]]>");
        item2.setLink("http://politics.people.com.cn/n1/2016/0707/c1001-28531678.html");
        item2.setPubDate("2016-07-07");
        item2.setAuthor("赵鹏");
        item2.setGuid("http://politics.people.com.cn/n1/2016/0707/c1001-28531678.html");
        item2.setDescription("<![CDATA[&amp;$   近日,国家发改委会同国家卫计委等四部门发布《关于印发推进医疗服务价格改革意见的通知》(以下简称《通知》)。通知指出,要降低大型医用设备检查治疗和检验价格,提高诊疗、手术、康复、护理、中医等体现医务人员技术劳务价值的医疗服务价格。此外,国家将负责制定全国医疗服务项目技术规范,统一项目名称和服务内容,指导医疗机构规范开展服务,并作为确定医疗机构收费项目的依据。    &gt;&gt;背景    近年来我国放开了非公立医疗机构医疗服务价格,围绕公立医院综合改革,调整医疗服务价格,促进了医疗机构新型补偿机制的建立。但受多种因素影响,医疗服务价格尚未完全理顺,管理方式仍需改进,价格行为]]>");
        NewItemList.add(item2);
    }
    public static void main(String[] args) {
        new DOM4JTest().createNewsItem();
        new DOM4JTest().createXML();
    }

ps:需要先创建一个NewItem实体类,其属性有:title、 link、pubDate、author;、guid、description。

生成的XML文件:

<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0">
  <channel>
    <title><![CDATA[时政频道]]></title>
    <image>
      <title><![CDATA[时政频道]]></title>
      <link>http://politics.people.cn</link>
      <url>http://www.people.cn/img/2014peoplelogo/rmw_logo.gif</url>
    </image>
    <description><![CDATA[时政新闻]]</description>
    <link>http://politics.people.cn</link>
    <language>zh-cn</language>
    <generator>www.people.cn</generator>
    <copyright><![CDATA[Copyright 1997-2016 by www.people.com.cn. all rights reserved]]></copyright>
    <pubDate>2016-07-07 22:03:00</pubDate>
    <item>
      <title><![CDATA[湖北入梅以来洪涝、风雹等灾害已造成直接损失270亿]]></title>
      <link>http://politics.people.com.cn/n1/2016/0707/c1001-28534758.html</link>
      <pubDate>2016-07-07</pubDate>
      <author>人民网</author>
      <guid>http://politics.people.com.cn/n1/2016/0707/c1001-28534758.html</guid>
      <description><![CDATA[&amp;$ 人民网北京7月7日电&nbsp;湖北省人民政府新闻办公室官方微博今日发布消息称,截至7月7日16时统计,6月18日入梅以来洪涝、风雹等自然灾害已造成全省17个市(州、直管市、林区)92个县(市、区)1781.26万人次受灾,死亡69人失踪16人,紧急转移安置73.2万人次,需紧急生活救助47.5万人次; 因灾倒塌房屋2.9万间,严重损坏房屋3.5万间,一般损坏3.8万间; 农作物受灾面积1558.3千公顷,其中绝收359.5千公顷; 直接经济损失270.7亿元。 &amp;$]]></description>
    </item>
    <item>
      <title><![CDATA[公立医院特需禁超全部服务10%]]></title>
      <link>http://politics.people.com.cn/n1/2016/0707/c1001-28531678.html</link>
      <pubDate>2016-07-07</pubDate>
      <author>赵鹏</author>
      <guid>http://politics.people.com.cn/n1/2016/0707/c1001-28531678.html</guid>
      <description><![CDATA[&amp;$   近日,国家发改委会同国家卫计委等四部门发布《关于印发推进医疗服务价格改革意见的通知》(以下简称《通知》)。通知指出,要降低大型医用设备检查治疗和检验价格,提高诊疗、手术、康复、护理、中医等体现医务人员技术劳务价值的医疗服务价格。此外,国家将负责制定全国医疗服务项目技术规范,统一项目名称和服务内容,指导医疗机构规范开展服务,并作为确定医疗机构收费项目的依据。   &gt;&gt;背景   近年来我国放开了非公立医疗机构医疗服务价格,围绕公立医院综合改革,调整医疗服务价格,促进了医疗机构新型补偿机制的建立。但受多种因素影响,医疗服务价格尚未完全理顺,管理方式仍需改进,价格行为]]></description>
    </item>
  </channel>
</rss>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值