在Jaxb中获取元素的Dynamic属性 [英] getting Dynamic attribute for element in Jaxb

查看:68
本文介绍了在Jaxb中获取元素的Dynamic属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下带有许多属性的XML标记。没有给出属性的数量/名称,因为我在运行时获取XML,而我只知道标记的名称。
如何使用JAXB将所有属性作为 Map< String,String>

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. How can I use JAXB to get all the attribute as a Map<String, String>?

如何将其添加到以下Java代码中:

How can I add this to the following Java code:

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "script ")
@XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD)
public class SearchScriptElement
{

    @XmlAttribute(name = "script")
    private String script = "";

    public String getScript()
    {
        return name;
    }

    public void setScript(String name)
    {
        this.name = name;
    }
}

XML示例:
我可以有很多运行时未知的属性:

XML example: I can have many attributes that aren't known in runtime:

<ScriptList>
    <script name="xxx" value="sss" id=100 >
    <script>
    <script name="xxx" value="sss" id=100 alias="sss">
    <script>
</ScriptList>


推荐答案

你可以这样做:

@XmlAnyAttribute
private Map<QName, String> attributes;

几乎 Map< String,String> 你想要的。

这篇关于在Jaxb中获取元素的Dynamic属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆