简体   繁体   English

将xml转换为hashmap,反之亦然

[英]Convert xml to hashmap and vice-versa

I have a xml schema with the following structure:我有一个具有以下结构的 xml 架构:

<?xml version="1.0" encoding="UTF-8"?>
<page>
   <body>
      <item attr1="a1" attr2="a2" />
      <item attr1="a1" attr2="a2" attr3="a3" attr4="a4" attr_n="a_n" />
      <item attr1="a1" attr2="a2" attr3="a3" attr4="a4" attr_n="a_n">
         <element1>abc</element1>
         <element2>xyz</element2>
         <element_n>nnn</element_n>
      </item>
   </body>
</page>

The item tag can have n number of attributes and elements, known or unknown.项目标签可以有 n 个已知或未知的属性和元素。 Also it may or may not have any element.此外,它可能有也可能没有任何元素。

I want to parse this xml into java pojo, I prefer using hashmap for the field item .我想将此 xml 解析为 java pojo,我更喜欢将 hashmap 用于字段item It parses rest of the things but the elements/attributes of item are not getting parsed.它解析其余的东西,但没有解析item的元素/属性。 It is always null.它始终为空。 Any suggestions on how to achieve this will be appreciated.任何有关如何实现这一目标的建议将不胜感激。

My pojo structure is like this:我的 pojo 结构是这样的:

Page.java页面.java


public class Page
{
    private Body body;

    public Body getBody ()
    {
        return body;
    }

    public void setBody (Body body)
    {
        this.body = body;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [body = "+body+"]";
    }
}

Body.java正文.java


public class Body
{
    private List<Map<String, Object>> item;

    public List<Map<String, Object>> getItem ()
    {
        return item;
    }

    public void setItem (List<Map<String, Object>> item)
    {
        this.item = item;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [item = "+item+"]";
    }
}

Create an xsd schema file.创建一个xsd架构文件。 You can get plugins in eclipse which can convert an xml to xsd .您可以在 eclipse 中获得插件,它可以将xml转换为xsd Using frameworks like jibx , jaxb etc create schema classes.使用jibxjaxb等框架创建模式类。 These libraries have classes to convert object to xml and vice-versa.这些库具有将对象转换为xml类,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将byte转换为int,反之亦然 - Convert byte to int and vice-versa 何时在 LinkedList 或 ArrayList 上使用 HashMap,反之亦然 - When to use HashMap over LinkedList or ArrayList and vice-versa 如何编写 Apache 骆驼路线来检查 XML 内容并将其转换为 JSON 和 Z3418008FDD81C28 引导服务反之亦然 - How to write a Apache Camel route to check and convert XML contents to JSON and vice-versa for a Spring boot service Java:如何将二进制值的字符串转换为Float,反之亦然? - Java: How to convert a String of Binary values to a Float and vice-versa? 如何将int []转换为OpenCV Mat? (反之亦然) - How to convert an int[] to an OpenCV Mat? (and vice-versa) 编年史队列:将循环整数转换为时间戳,反之亦然 - Chronicle Queue: Convert cycle integer to timestamp and vice-versa 有多少种方法可以将位图转换为字符串,反之亦然? - How many ways to convert bitmap to string and vice-versa? 在MySQL上受益还是使用XML,反之亦然? - Benefits or using XML over MySQL and vice-versa? 如何将缓冲的图像转换为图像,反之亦然? - How to convert buffered image to image and vice-versa? 将 DTO 转换为实体,反之亦然 - Conversion of DTO to entity and vice-versa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM