java - How to get both the attributes and the data list with XmlList -
i have xml files contain elements so:
<element attribute1="a" attribute2="b" attribute3="c"> b c d e f g </element>
is there way both attributes , value list?
i use @xmllist list [a, b, c, d, e, f, g], not have attributes. make class element , use @xmlattribute , @xmlvalue, value not list.
if there no way this, make class element in getter method returns string value array or list, , simple enough, wondering if there correct way have xml unmarshalled in first place.
the @xmlvalue
can applied list
property. mapping list items represented space separated list in xml. can following:
element
import java.util.list; import javax.xml.bind.annotation.*; @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class element { @xmlattribute private string attribute1; @xmlattribute private string attribute2; @xmlattribute private string attribute3; @xmlvalue private list<string> value; }
demo
import java.io.file; import javax.xml.bind.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(element.class); unmarshaller unmarshaller = jc.createunmarshaller(); file xml = new file("src/forum17775900/input.xml"); element element = (element) unmarshaller.unmarshal(xml); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(element, system.out); } }
input.xml/output
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <element attribute1="a" attribute2="b" attribute3="c">a b c d e f g</element>
Comments
Post a Comment