Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. <Attributes> <Map> <entry key="AE" value="never"/> <entry key="AF"> <value> <List> <String>NUA</String> </List> </value> </entry> </Map></Attributes>
  2.  
  3. import java.util.HashMap;
  4. import javax.xml.bind.annotation.XmlElement;
  5. import javax.xml.bind.annotation.XmlRootElement;
  6. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  7.  
  8. @XmlRootElement(name = "Attributes")
  9. public class Attributes {
  10.  
  11. @XmlJavaTypeAdapter(MapAdapter.class)
  12. private HashMap<String, Object> Map;
  13.  
  14. @XmlElement(name = "Map")
  15. public void setMap(HashMap<String, Object> map) {
  16. Map = map;
  17. }
  18.  
  19. public HashMap<String, Object> getMap() {
  20. return Map;
  21. }
  22.  
  23. public Attributes() {
  24. }
  25. }
  26.  
  27. import java.util.List;
  28.  
  29. import javax.xml.bind.annotation.XmlElement;
  30.  
  31. public class Map {
  32.  
  33. public Map() {
  34. }
  35.  
  36. private List<Entry> entries;
  37.  
  38. public List<Entry> getEntries() {
  39. return entries;
  40. }
  41.  
  42. public void addEntry(Entry entry) {
  43. entries.add(entry);
  44. }
  45.  
  46. @XmlElement(name = "entry")
  47. public void setEntries(List<Entry> entries) {
  48. this.entries = entries;
  49. }
  50.  
  51. }
  52.  
  53. import javax.xml.bind.annotation.XmlAttribute;
  54. import javax.xml.bind.annotation.XmlRootElement;
  55.  
  56. @XmlRootElement(name = "Entry")
  57. public class Entry {
  58.  
  59. private String key;
  60.  
  61. @XmlAttribute(name = "key")
  62. public void setKey(String key) {
  63. this.key = key;
  64. }
  65.  
  66. public String getKey() {
  67. return key;
  68. }
  69.  
  70. @XmlAttribute(name = "value")
  71. public void setValue(String value) {
  72. this.value = value;
  73. }
  74.  
  75. private String value;
  76.  
  77. public String getValue() {
  78. return value;
  79. }
  80.  
  81. private Entry() {
  82. }
  83.  
  84. public Entry(String key, String value) {
  85. this.key = key;
  86. this.value = value;
  87. }
  88.  
  89. }
  90.  
  91. import java.util.Collection;
  92. import java.util.HashMap;
  93. import java.util.Map;
  94.  
  95. import javax.xml.bind.annotation.adapters.XmlAdapter;
  96.  
  97. public class MapAdapter extends XmlAdapter<Preprocessor.mapAttributes.Map, Map<String, Entry>> {
  98. @Override
  99. public Map<String, Entry> unmarshal(Preprocessor.mapAttributes.Map value) {
  100. Map<String, Entry> map = new HashMap<String, Entry>();
  101. for (Entry entry : value.getEntries()) {
  102. map.put(entry.getKey(), entry);
  103. }
  104. return map;
  105. }
  106.  
  107. @Override
  108. public Preprocessor.mapAttributes.Map marshal(Map<String, Entry> map) {
  109. Preprocessor.mapAttributes.Map msgCont = new Preprocessor.mapAttributes.Map();
  110. Collection<Entry> msgs = map.values();
  111. msgCont.getEntries().addAll(msgs);
  112. return msgCont;
  113. }
  114. }
  115.  
  116. import java.io.File;
  117. import java.io.StringReader;
  118. import java.sql.Connection;
  119. import java.sql.Statement;
  120. import java.util.Map;
  121. import java.util.Map.Entry;
  122.  
  123. import javax.xml.XMLConstants;
  124. import javax.xml.bind.JAXBContext;
  125. import javax.xml.bind.JAXBException;
  126. import javax.xml.bind.Unmarshaller;
  127. import javax.xml.validation.Schema;
  128. import javax.xml.validation.SchemaFactory;
  129.  
  130. import org.xml.sax.SAXException;
  131.  
  132. import Preprocessor.mapAttributes.Attributes;
  133. import Preprocessor.mapAttributes.MapAdapter;
  134.  
  135. public class Main {
  136.  
  137. private static Connection conn = null;
  138. private static Statement stmt = null;
  139. private static JAXBContext jc = null;
  140. private static Schema schema = null;
  141.  
  142. public Main() {
  143. try {
  144. jc = JAXBContext.newInstance(Attributes.class, MapAdapter.class, Preprocessor.mapAttributes.Map.class,
  145. Preprocessor.mapAttributes.Entry.class, Preprocessor.mapAttributes.Value.class);
  146.  
  147. SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  148. try {
  149. schema = sf.newSchema(new File("C:/Users/Darang Dawer/Desktop/schema.xsd"));
  150. } catch (SAXException e) {
  151. throw new RuntimeException("Error while initializing schema... ", e);
  152. }
  153. } catch (JAXBException e) {
  154. throw new RuntimeException("Error initializing jaxB context", e);
  155. }
  156. }
  157.  
  158. public static void main(String args[]) {
  159.  
  160. Main a = new Main();
  161. a.parse();
  162.  
  163. }
  164.  
  165. private void parse() {
  166.  
  167. String sampleParsing = "<Attributes> <Map> <entry key="AE" value="never"/> <entry key="AF"> <value> <List> <String>NUA</String> </List> </value> </entry> </Map></Attributes>";
  168.  
  169. parseJAXB(sampleParsing);
  170.  
  171. }
  172.  
  173. private void parseJAXB(String sampleParsing) {
  174. {
  175. try {
  176.  
  177. Unmarshaller unmarshaller = jc.createUnmarshaller();
  178. MapAdapter mapAdapter = new MapAdapter();
  179. unmarshaller.setAdapter(mapAdapter);
  180. Attributes attributes = (Attributes) unmarshaller.unmarshal(new StringReader(sampleParsing));
  181.  
  182. // System.out.println(attributes.getMap());
  183. Map<String, Object> map = attributes.getMap();
  184. // System.out.println(map);
  185. for (Entry<String, Object> key : map.entrySet()) {
  186. System.out.println(key.getKey() + " | "
  187. // + (key.getValue()));
  188. + ((Preprocessor.mapAttributes.Entry) key.getValue()).getValue());
  189. }
  190.  
  191. } catch (JAXBException e) {
  192. throw new RuntimeException(e);
  193. }
  194.  
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement