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>
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 .
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.
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.
You can do:
@XmlAnyAttribute
private Map<QName, String> attributes;
Almost the Map<String, String>
that you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With