getting Dynamic attribute for element in Jaxb

前端 未结 2 515
既然无缘
既然无缘 2020-12-20 23:23

I have the following XML tag with many attributes. The number/name of the attributes is not given because I am getting the XML in runtime and I just know the name of the tag

相关标签:
2条回答
  • 2020-12-20 23:53

    Create 2 classes ScriptList and Script:

    @XmlType(name = "ScriptList")
    public class ScriptList {
        private Collection<Script> scripts;
    
        @XmlElement(name = "location")
        public Collection<Script> getSripts() {
            return scripts;
        }
    }
    
    @XmlType(name = "script")
    public class Script {
        private String name;
        private String value;
        private String id;
        private String alias;
    
        @XmlAttribute(name="name")
        public String getName() {
            return name;
        }
        // add similar getters for value, id, alias.
        // add setters for all fields.
    }
    

    I believe, that's all. At least this can be your starting point.

    0 讨论(0)
  • 2020-12-21 00:01

    You can do:

    @XmlAnyAttribute
    private Map<QName, String> attributes;
    

    Almost the Map<String, String> that you wanted.

    0 讨论(0)
提交回复
热议问题