Java学习笔记39:Spring XML使用
来源:互联网 发布:手机点位图软件 编辑:程序博客网 时间:2024/06/05 08:44
我们使用jdom的XML解析库,所以pom.xml中需要依赖:
<!-- XML解析 --><dependency><groupId>org.jdom</groupId><artifactId>jdom</artifactId><version>2.0.2</version></dependency>
创建XML实例:
Element rootElement = new Element("root"); //创建根节点 //属性创建 rootElement.setAttribute("title", "this is test xml"); //root节点上穿件一个title属性 rootElement.setAttribute("id", "root"); //root节点上穿件一个title属性 //创建person1子节点 Element person1 = new Element("person1"); //创建person1节点 person1.addContent(new Element("name").setText("zhuli")); //创建person1中的name节点和内容 person1.addContent(new Element("age").setText("30岁"));//创建person1中的age节点和内容 //创建person2子节点 Element person2 = new Element("person2"); //创建person2节点 person2.addContent(new Element("name").setText("zhuli")); //创建person2中的name节点和内容 person2.addContent(new Element("age").setText("30岁"));//创建person2中的age节点和内容 //root节点上面添加子节点 rootElement.addContent(person1); //将person1节点加入到root根节点 rootElement.addContent(person2); //将person1节点加入到root根节点 rootElement.addContent(new Element("result").setText("ss").setAttribute("id", "result")); //只有一层的子节点 rootElement.addContent(new Element("detail").setText("asdasda")); Document myDocument = new Document(rootElement); XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()); return xmlOut.outputString(myDocument);
结果:
<?xml version="1.0" encoding="UTF-8"?><root title="this is test xml" id="root"> <person1> <name>zhuli</name> <age>30岁</age> </person1> <person2> <name>zhuli</name> <age>30岁</age> </person2> <result id="result">ss</result> <detail>asdasda</detail></root>
解析XML实例:
解析上面的xml文档
@SuppressWarnings("deprecation") SAXBuilder builder = new SAXBuilder(false); try { InputStream isXml = new ByteArrayInputStream(xml.getBytes("UTF-8")); Document document = builder.build(isXml); //获取XML文档实例,字符串XML格式,如果是URL是builder.url() Element root = document.getRootElement(); //获取根节点 //获取根节点root上的title和id属性 System.out.println("root title:" + root.getAttributeValue("title")); System.out.println("root id:" + root.getAttributeValue("id")); //person1节点: System.out.println("person1节点:"); Element person1 = root.getChild("person1"); System.out.println("person1 name:" + person1.getChildText("name")); System.out.println("person1 age:" + person1.getChildText("age")); //person2节点: System.out.println("person2节点:"); Element person2 = root.getChild("person2"); System.out.println("person2 name:" + person2.getChildText("name")); System.out.println("person2 age:" + person2.getChildText("age")); //获取result值 System.out.println("result text:" + root.getChildText("result")); // System.out.println("detail text:" + root.getChildText("detail")); } catch (Exception e) { }
控制台结果:
root title:this is test xmlroot id:rootperson1节点:person1 name:zhuliperson1 age:30岁person2节点:person2 name:zhuliperson2 age:30岁result text:ssdetail text:asdasda
- Java学习笔记39:Spring XML使用
- java学习笔记:使用dom4j解析xml
- spring 学习笔记 使用pojo+xml的方式开发aop
- 【spring学习笔记一】使用xml文件配置bean
- Java 学习笔记04:Spring XML配置文件Bean
- Java & XML学习笔记
- [Java] Spring 学习笔记
- java spring学习笔记
- JAVA-Spring学习笔记
- 【学习笔记】JAVA之使用dom4j对xml文件解析
- 【学习笔记】JAVA之使用SAX解析XML文件
- spring学习笔记(8)--xml annotation
- spring学习笔记(15)--AOP XML
- Spring学习笔记之最小化xml配置
- Spring框架学习笔记----xml配置
- spring学习笔记---------applicationContext.xml 1
- java学习笔记——使用DOM解析XML和使用SAX解析XML
- [XML学习笔记][2]XML的使用
- 最圆点对,(凸包+旋转卡壳)
- treeview checkbox选中的时候选择子节点 mvvm
- phonegap入门--8 Compass 罗盘
- 开发者使用JasperReport——不同数据源之实体对象数据源
- sql删除重复数据
- Java学习笔记39:Spring XML使用
- 排序算法系列之二叉查找树
- 手机网站开发——网页音频播放器(续)
- Java文件的 In 与 Out 的相对性
- snd_kcontrol探究
- PHPRPC模式在 php 5.4 版本下的错误Cannot redeclare gzdecode() in compat.php on line 182
- snd_kcontrol_new名称中的SOURCE字段
- windows7下笔记本如何做热点
- Linux下chkconfig命令详解