Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting Dynamic attribute for element in Jaxb

Tags:

java

jaxb

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

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 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>
like image 876
user1205079 Avatar asked Mar 11 '12 15:03

user1205079


People also ask

What is the use of @XmlRootElement annotation?

When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document. This annotation can be used with the following annotations: XmlType , XmlEnum , XmlAccessorType , XmlAccessorOrder .

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

What is JAXB marshalling and Unmarshalling?

JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.


1 Answers

You can do:

@XmlAnyAttribute
private Map<QName, String> attributes;

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

like image 193
lexicore Avatar answered Sep 28 '22 08:09

lexicore