Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Attributes> <Map> <entry key="AE" value="never"/> <entry key="AF"> <value> <List> <String>NUA</String> </List> </value> </entry> </Map></Attributes>
- import java.util.HashMap;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlRootElement;
- import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
- @XmlRootElement(name = "Attributes")
- public class Attributes {
- @XmlJavaTypeAdapter(MapAdapter.class)
- private HashMap<String, Object> Map;
- @XmlElement(name = "Map")
- public void setMap(HashMap<String, Object> map) {
- Map = map;
- }
- public HashMap<String, Object> getMap() {
- return Map;
- }
- public Attributes() {
- }
- }
- import java.util.List;
- import javax.xml.bind.annotation.XmlElement;
- public class Map {
- public Map() {
- }
- private List<Entry> entries;
- public List<Entry> getEntries() {
- return entries;
- }
- public void addEntry(Entry entry) {
- entries.add(entry);
- }
- @XmlElement(name = "entry")
- public void setEntries(List<Entry> entries) {
- this.entries = entries;
- }
- }
- import javax.xml.bind.annotation.XmlAttribute;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(name = "Entry")
- public class Entry {
- private String key;
- @XmlAttribute(name = "key")
- public void setKey(String key) {
- this.key = key;
- }
- public String getKey() {
- return key;
- }
- @XmlAttribute(name = "value")
- public void setValue(String value) {
- this.value = value;
- }
- private String value;
- public String getValue() {
- return value;
- }
- private Entry() {
- }
- public Entry(String key, String value) {
- this.key = key;
- this.value = value;
- }
- }
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Map;
- import javax.xml.bind.annotation.adapters.XmlAdapter;
- public class MapAdapter extends XmlAdapter<Preprocessor.mapAttributes.Map, Map<String, Entry>> {
- @Override
- public Map<String, Entry> unmarshal(Preprocessor.mapAttributes.Map value) {
- Map<String, Entry> map = new HashMap<String, Entry>();
- for (Entry entry : value.getEntries()) {
- map.put(entry.getKey(), entry);
- }
- return map;
- }
- @Override
- public Preprocessor.mapAttributes.Map marshal(Map<String, Entry> map) {
- Preprocessor.mapAttributes.Map msgCont = new Preprocessor.mapAttributes.Map();
- Collection<Entry> msgs = map.values();
- msgCont.getEntries().addAll(msgs);
- return msgCont;
- }
- }
- import java.io.File;
- import java.io.StringReader;
- import java.sql.Connection;
- import java.sql.Statement;
- import java.util.Map;
- import java.util.Map.Entry;
- import javax.xml.XMLConstants;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Unmarshaller;
- import javax.xml.validation.Schema;
- import javax.xml.validation.SchemaFactory;
- import org.xml.sax.SAXException;
- import Preprocessor.mapAttributes.Attributes;
- import Preprocessor.mapAttributes.MapAdapter;
- public class Main {
- private static Connection conn = null;
- private static Statement stmt = null;
- private static JAXBContext jc = null;
- private static Schema schema = null;
- public Main() {
- try {
- jc = JAXBContext.newInstance(Attributes.class, MapAdapter.class, Preprocessor.mapAttributes.Map.class,
- Preprocessor.mapAttributes.Entry.class, Preprocessor.mapAttributes.Value.class);
- SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
- try {
- schema = sf.newSchema(new File("C:/Users/Darang Dawer/Desktop/schema.xsd"));
- } catch (SAXException e) {
- throw new RuntimeException("Error while initializing schema... ", e);
- }
- } catch (JAXBException e) {
- throw new RuntimeException("Error initializing jaxB context", e);
- }
- }
- public static void main(String args[]) {
- Main a = new Main();
- a.parse();
- }
- private void parse() {
- String sampleParsing = "<Attributes> <Map> <entry key="AE" value="never"/> <entry key="AF"> <value> <List> <String>NUA</String> </List> </value> </entry> </Map></Attributes>";
- parseJAXB(sampleParsing);
- }
- private void parseJAXB(String sampleParsing) {
- {
- try {
- Unmarshaller unmarshaller = jc.createUnmarshaller();
- MapAdapter mapAdapter = new MapAdapter();
- unmarshaller.setAdapter(mapAdapter);
- Attributes attributes = (Attributes) unmarshaller.unmarshal(new StringReader(sampleParsing));
- // System.out.println(attributes.getMap());
- Map<String, Object> map = attributes.getMap();
- // System.out.println(map);
- for (Entry<String, Object> key : map.entrySet()) {
- System.out.println(key.getKey() + " | "
- // + (key.getValue()));
- + ((Preprocessor.mapAttributes.Entry) key.getValue()).getValue());
- }
- } catch (JAXBException e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement