Java解析XML

xml报文

1<?xml version='1.0' encoding='utf-8'?> 2<bookstore> 3 <book id='1'> 4 <name>冰与火之歌</name> 5 <author>乔治马丁</author> 6 <year>2014</year> 7 <price>89</price> 8 </book> 9 <book id='2'> 10 <name>安徒生童话</name> 11 <author>安徒生</author> 12 <year>2014</year> 13 <price>69</price> 14 </book> 15</bookstore>

java代码:

1 1 import java.util.HashMap; 2 2 import java.util.List; 3 3 import java.util.Map; 4 4 5 5 6 6 import com.alibaba.fastjson.JSONArray; 7 7 import com.alibaba.fastjson.JSONObject; 8 8 import org.dom4j.Document; 9 9 import org.dom4j.DocumentException; 1010 import org.dom4j.DocumentHelper; 1111 import org.dom4j.Element; 1212 1313 public class XmlTest { 1414 public static Element getChildElement(Element e, String element_name) { 1515 Element returnElement = null; 1616 List<Element> elements = e.elements(); 1717 if (elements != null && !elements.isEmpty()) { 1818 for (Element element : elements) { 1919 if (element.getName().equals(element_name)) { 2020 returnElement = element; 2121 break; 2222 } 2323 } 2424 } 2525 return returnElement; 2626 } 2727 2828 public static void main(String[] args) { 2929 String resp = "<?xml version='1.0' encoding='UTF-8'?><bookstore><book id='1'><name>冰与火之歌</name><author>乔治马丁</author><year>2014</year><price>89</price></book><book id='2'><name>安徒生童话</name><author>安徒生</author><year>2014</year><price>69</price></book></bookstore>"; 3030 3131 try { 3232 Document doc = DocumentHelper.parseText(resp); 3333 //指向根节点 3434 Element root = doc.getRootElement(); 3535 JSONObject json = null; 3636 JSONArray jsonArray = new JSONArray(); 3737 // Element book = XMLString2.getChildElement(root, "book"); 3838 //获取book的属性值 3939 // String id = book.attributeValue("id"); 4040 // kBook.setId(id); 4141 List<Element> elements = root.elements(); 4242 for (Element element : elements) { 4343 Map <String,Object> map = new HashMap<>(); 4444 map.put("name",element.element("name").getStringValue()); 4545 map.put("author", element.element("author").getStringValue()); 4646 map.put("price", element.element("price").getStringValue()); 4747 map.put("year", element.element("year").getStringValue()); 4848 json = new JSONObject(map); 4949 jsonArray.add(json); 5050 } 5151 5252 5353 } catch (DocumentException e) { 5454 // TODO Auto-generated catch block 5555 e.printStackTrace(); 5656 } 5757 5858 } 5959 6060 }

结果

 参考博客:https://blog.csdn.net/zflovecf/article/details/78908788

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Java爬虫之JSoup使用教程

title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin