• Java解析XML


    xml报文

    <?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>

    java代码:

     1 import java.util.HashMap;
     2 import java.util.List;
     3 import java.util.Map;
     4 
     5 
     6 import com.alibaba.fastjson.JSONArray;
     7 import com.alibaba.fastjson.JSONObject;
     8 import org.dom4j.Document;
     9 import org.dom4j.DocumentException;
    10 import org.dom4j.DocumentHelper;
    11 import org.dom4j.Element;
    12 
    13 public class XmlTest {
    14     public static Element getChildElement(Element e, String element_name) {
    15         Element returnElement = null;
    16         List<Element> elements = e.elements();
    17         if (elements != null && !elements.isEmpty()) {
    18             for (Element element : elements) {
    19                 if (element.getName().equals(element_name)) {
    20                     returnElement = element;
    21                     break;
    22                 }
    23             }
    24         }
    25         return returnElement;
    26     }
    27 
    28     public static void main(String[] args) {
    29         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>";
    30 
    31         try {
    32             Document doc = DocumentHelper.parseText(resp);
    33             //指向根节点
    34             Element root = doc.getRootElement();
    35             JSONObject json = null;
    36             JSONArray jsonArray = new JSONArray();
    37 //            Element book = XMLString2.getChildElement(root, "book");
    38             //获取book的属性值
    39 //            String id = book.attributeValue("id");
    40 //            kBook.setId(id);
    41             List<Element> elements = root.elements();
    42             for (Element element : elements) {
    43                 Map <String,Object> map = new HashMap<>();
    44                 map.put("name",element.element("name").getStringValue());
    45                 map.put("author", element.element("author").getStringValue());
    46                 map.put("price", element.element("price").getStringValue());
    47                 map.put("year", element.element("year").getStringValue());
    48                 json = new JSONObject(map);
    49                 jsonArray.add(json);
    50             }
    51 
    52 
    53         } catch (DocumentException e) {
    54             // TODO Auto-generated catch block
    55             e.printStackTrace();
    56         }
    57 
    58     }
    59 
    60 }

    结果

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

  • 相关阅读:
    RabbitMQ入门-消息订阅模式
    RabbitMQ入门-消息派发那些事儿
    RabbitMQ入门-高效的Work模式
    RabbitMQ入门-从HelloWorld开始
    RabbitMQ入门-初识RabbitMQ
    CMake INSTALL 命令设置exe dll lib的安装位置
    VS调试DLL代码使用”附加到进程“
    模型自身面片重合引起的闪烁破损解决方法
    地球表面使用世界坐标系绘制物体闪烁破损处理方法
    3dmax osg格式导出插件 osgExp OpenSceneGraph Max Exporter
  • 原文地址:https://www.cnblogs.com/hoje/p/11868163.html
Copyright © 2020-2023  润新知