加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

教大家快捷的生成和解析xml

发布时间:2020-12-16 05:28:31 所属栏目:百科 来源:网络整理
导读:做java开发的兄弟姐妹们都知道生成解析xml一般有四大组件:dom、jdom、dom4j和sax;虽然这些组件比较成熟,而且性能也比较稳定了,但是在实际项目开发过程中有时候需要快速的生成或者解析一段xml字符串,用这些传统组件的话代码量往往比较大,今天给大家介绍

做java开发的兄弟姐妹们都知道生成解析xml一般有四大组件:dom、jdom、dom4j和sax;虽然这些组件比较成熟,而且性能也比较稳定了,但是在实际项目开发过程中有时候需要快速的生成或者解析一段xml字符串,用这些传统组件的话代码量往往比较大,今天给大家介绍一种工具包xmlzen.jar,它可以快速的生成和解析一段xml;

/*
 * To change this template,choose Tools | Templates
 * and open the template in the editor.
 */
package xmlgen;

import com.googlecode.xmlzen.XmlBuilder;
import com.googlecode.xmlzen.XmlSlicer;

/**
 *
 * @author xuweilin
 */
public class XmlGen {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String xml = 
                XmlBuilder.newXml("UTF-8",true).
                openTag("root").withAttribute("rootId",1).
                openTag("database").withValue("Mysql数据库").closeTag().
                openTag("developeTool").withAttribute("type","软件").withAttribute("name","java").closeTag().
                openTag("subList").openTag("people1").withValue("张三").closeTag().openTag("people2").withValue("李四").closeTag().closeTag().
                openTag("data").withCDATA("测试一下").closeTag().
                toString(true);
        System.out.println(xml);
        System.out.println("################################################");
        String value = XmlSlicer.cut(xml).getTagAttribute("developeTool","type").toString();
        String people1 = XmlSlicer.cut(xml).get("people1").toString();
        System.out.println("developeTool>type:"+value);
        System.out.println("people1:"+value);
    }
}

运行结果截图如下

jar包下载地址:http://dl.vmall.com/c0txj2m2bm

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读