做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