簡體   English   中英

使用簡單的 XML 將 HashMap 解析為 xml - JAVA

[英]parsing HashMap into xml with Simple XML - JAVA

我需要將值放在 pat 中作為我的 Ex 的 XML 文件的值:

Map<String, String> props = new HashMap<>();
props.put("role", "Admin");
props.put("externalId", "2ew1Q");
props.put("Property", "internal");
props.put("Execution", "internal");

我的預期輸出應該是:

<role>Admin</role>
<externalId>2ew1Q</externalId>
<Property>internal</Property>
<Execution>internal</Execution>

但取而代之的是,我得到

<entry string="role">Admin</entry>
<entry string="Execution">internal</entry>
<entry string="externalId">2ew1Q</entry>
<entry string="Property">internal</entry>

我必須用簡單的 XML 來做,這是我的代碼:

    @Root
    public class Data {
    
        @ElementMap(entry = "property", key = "key", attribute = true, inline = true)
        private Map<String, String> customProps;
    
        public Map<String, String> getData() {
            return customProps;
        }
    
        public void setData(Map<String, String> data) {
            this.customProps = data;
        }
    }


public static void main(String[] args) throws Exception {

        Map<String, String> props = new HashMap<>();

        props.put("role", "Admin");
        props.put("externalId", "2ew1Q");
        props.put("Property", "internal");
        props.put("Execution", "internal");


        Data customProps = new Data();
        customProps.setData(props);

        Serializer serializer = new Persister();
        File result = new File("example.xml");
        serializer.write(customProps, result);
}

試試: @ElementMap(entry = "property", key = "key", attribute = false, inline = true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM