Guest User

Untitled

a guest
Feb 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 KB | None | 0 0
  1. <Test>
  2. <Item>
  3. <Group>
  4. <SubGroup test4="2018-09-01" test5="Y">
  5.  
  6. </SubGroup>
  7. </Group>
  8. </Item>
  9. <Item>
  10. <Group>
  11. <SubGroup test="0" test2="t">
  12.  
  13. </SubGroup>
  14. <SubGroup test3="0.0">
  15.  
  16. </SubGroup>
  17. </Group>
  18. </Item>
  19. </Test>
  20.  
  21. import org.w3c.dom.Node;
  22. import org.w3c.dom.NodeList;
  23.  
  24. import java.text.ParseException;
  25. import java.text.SimpleDateFormat;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28.  
  29. public class UtilityMethods {
  30. public static byte getInt8(String input) {
  31. if (input == null || input.equals("")) {
  32. return 0;
  33. } else {
  34. return Byte.parseByte(input);
  35. }
  36. }
  37. public static short getInt16(String input) {
  38. if (input == null || input.equals("")) {
  39. return 0;
  40. } else {
  41. return Short.parseShort(input);
  42. }
  43. }
  44. public static int getInt32(String input) {
  45. if (input == null || input.equals("")) {
  46. return 0;
  47. } else {
  48. return Integer.parseInt(input);
  49. }
  50. }
  51. public static long getInt64(String input) {
  52. if (input == null || input.equals("")) {
  53. return 0L;
  54. } else {
  55. return Long.parseLong(input);
  56. }
  57. }
  58. public static Date getDate(String input) throws ParseException {
  59. if (input == null || input.equals("")) {
  60. return new Date();
  61. } else {
  62. return new SimpleDateFormat("yyyy-MM-dd").parse(input);
  63. }
  64. }
  65. public static double getDouble(String input) {
  66. if (input == null || input.equals("")) {
  67. return 0.0;
  68. } else {
  69. return Double.parseDouble(input);
  70. }
  71. }
  72. public static boolean getBoolean(String input) {
  73. if (input == null || input.equals("")) {
  74. return false;
  75. } else {
  76. return (input.equals("y") ? true : input.equals("yes") ? true : false);
  77. }
  78. }
  79.  
  80. public static ArrayList<Node> getNodesByTagName(Node rootNode, String tagName) {
  81. ArrayList<Node> result = new ArrayList<>();
  82. NodeList subNodes = rootNode.getChildNodes();
  83.  
  84. for (int i = 0; i < subNodes.getLength(); i++) {
  85. if (subNodes.item(i).getNodeName().equals(tagName)) {
  86. result.add(subNodes.item(i));
  87. }
  88. }
  89.  
  90. return result;
  91. }
  92.  
  93. public static String getAttrValue(Node attr) {
  94. if (attr == null) {
  95. return null;
  96. } else {
  97. return attr.getNodeValue();
  98. }
  99. }
  100. }
  101.  
  102. import org.w3c.dom.Document;
  103. import org.w3c.dom.Element;
  104. import org.w3c.dom.Node;
  105. import org.w3c.dom.NodeList;
  106. import org.xml.sax.SAXException;
  107.  
  108. import javax.xml.parsers.DocumentBuilder;
  109. import javax.xml.parsers.DocumentBuilderFactory;
  110. import javax.xml.parsers.ParserConfigurationException;
  111. import java.io.ByteArrayInputStream;
  112. import java.io.IOException;
  113. import java.text.ParseException;
  114. import java.util.ArrayList;
  115. import java.util.Date;
  116.  
  117. public class SubGroup {
  118. private Date _test4;
  119. public Date get_test4() { return _test4; }
  120. public void set_test4(Date test4) { _test4 = test4; }
  121.  
  122. private boolean _test5;
  123. public boolean get_test5() { return _test5; }
  124. public void set_test5(boolean test5) { _test5 = test5; }
  125.  
  126. private String __Text;
  127. public String get__Text() { return __Text; }
  128. public void set__Text(String _Text) { __Text = _Text; }
  129.  
  130. private String _test2;
  131. public String get_test2() { return _test2; }
  132. public void set_test2(String test2) { _test2 = test2; }
  133.  
  134. private byte _test;
  135. public byte get_test() { return _test; }
  136. public void set_test(byte test) { _test = test; }
  137.  
  138. private double _test3;
  139. public double get_test3() { return _test3; }
  140. public void set_test3(double test3) { _test3 = test3; }
  141.  
  142. public void deserialize(String xml) throws ParserConfigurationException, IOException, SAXException, ParseException {
  143. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  144. DocumentBuilder documentBuilder = factory.newDocumentBuilder();
  145. Document doc = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes("utf-8")));
  146. Element root = doc.getDocumentElement();
  147. deserialize(doc.getElementsByTagName("SubGroup").item(0));
  148. }
  149.  
  150. public void deserialize(Node myNode) throws ParseException {
  151. NodeList nodes;
  152. _test4 = UtilityMethods.getDate(UtilityMethods.getAttrValue(myNode.getAttributes().getNamedItem("test4")));
  153. _test5 = UtilityMethods.getBoolean(UtilityMethods.getAttrValue(myNode.getAttributes().getNamedItem("test5")));
  154. __Text = myNode.getTextContent();
  155. _test2 = UtilityMethods.getAttrValue(myNode.getAttributes().getNamedItem("test2"));
  156. _test = UtilityMethods.getInt8(UtilityMethods.getAttrValue(myNode.getAttributes().getNamedItem("test")));
  157. _test3 = UtilityMethods.getDouble(UtilityMethods.getAttrValue(myNode.getAttributes().getNamedItem("test3")));
  158. }
  159. }
  160.  
  161. import org.w3c.dom.Document;
  162. import org.w3c.dom.Element;
  163. import org.w3c.dom.Node;
  164. import org.w3c.dom.NodeList;
  165. import org.xml.sax.SAXException;
  166.  
  167. import javax.xml.parsers.DocumentBuilder;
  168. import javax.xml.parsers.DocumentBuilderFactory;
  169. import javax.xml.parsers.ParserConfigurationException;
  170. import java.io.ByteArrayInputStream;
  171. import java.io.IOException;
  172. import java.text.ParseException;
  173. import java.util.ArrayList;
  174. import java.util.Date;
  175.  
  176. public class Group {
  177. private ArrayList<SubGroup> _subGroup;
  178. public ArrayList<SubGroup> get_subGroup() { return _subGroup; }
  179. public void set_subGroup(ArrayList<SubGroup> subGroup) { _subGroup = subGroup; }
  180.  
  181. public void deserialize(String xml) throws ParserConfigurationException, IOException, SAXException, ParseException {
  182. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  183. DocumentBuilder documentBuilder = factory.newDocumentBuilder();
  184. Document doc = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes("utf-8")));
  185. Element root = doc.getDocumentElement();
  186. deserialize(doc.getElementsByTagName("Group").item(0));
  187. }
  188.  
  189. public void deserialize(Node myNode) throws ParseException {
  190. NodeList nodes;
  191. _subGroup = new ArrayList<>();
  192. for (Node node : UtilityMethods.getNodesByTagName(myNode, "SubGroup")) {
  193. SubGroup item;
  194. item = new SubGroup();
  195. item.deserialize(node);
  196. _subGroup.add(item);
  197. }
  198. }
  199. }
  200.  
  201. import org.w3c.dom.Document;
  202. import org.w3c.dom.Element;
  203. import org.w3c.dom.Node;
  204. import org.w3c.dom.NodeList;
  205. import org.xml.sax.SAXException;
  206.  
  207. import javax.xml.parsers.DocumentBuilder;
  208. import javax.xml.parsers.DocumentBuilderFactory;
  209. import javax.xml.parsers.ParserConfigurationException;
  210. import java.io.ByteArrayInputStream;
  211. import java.io.IOException;
  212. import java.text.ParseException;
  213. import java.util.ArrayList;
  214. import java.util.Date;
  215.  
  216. public class Item {
  217. private Group _group;
  218. public Group get_group() { return _group; }
  219. public void set_group(Group group) { _group = group; }
  220.  
  221. public void deserialize(String xml) throws ParserConfigurationException, IOException, SAXException, ParseException {
  222. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  223. DocumentBuilder documentBuilder = factory.newDocumentBuilder();
  224. Document doc = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes("utf-8")));
  225. Element root = doc.getDocumentElement();
  226. deserialize(doc.getElementsByTagName("Item").item(0));
  227. }
  228.  
  229. public void deserialize(Node myNode) throws ParseException {
  230. NodeList nodes;
  231. _group = new Group();
  232. _group.deserialize(UtilityMethods.getNodesByTagName(myNode, "Group").get(0));
  233. }
  234. }
  235.  
  236. import org.w3c.dom.Document;
  237. import org.w3c.dom.Element;
  238. import org.w3c.dom.Node;
  239. import org.w3c.dom.NodeList;
  240. import org.xml.sax.SAXException;
  241.  
  242. import javax.xml.parsers.DocumentBuilder;
  243. import javax.xml.parsers.DocumentBuilderFactory;
  244. import javax.xml.parsers.ParserConfigurationException;
  245. import java.io.ByteArrayInputStream;
  246. import java.io.IOException;
  247. import java.text.ParseException;
  248. import java.util.ArrayList;
  249. import java.util.Date;
  250.  
  251. public class Test {
  252. private ArrayList<Item> _item;
  253. public ArrayList<Item> get_item() { return _item; }
  254. public void set_item(ArrayList<Item> item) { _item = item; }
  255.  
  256. public void deserialize(String xml) throws ParserConfigurationException, IOException, SAXException, ParseException {
  257. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  258. DocumentBuilder documentBuilder = factory.newDocumentBuilder();
  259. Document doc = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes("utf-8")));
  260. Element root = doc.getDocumentElement();
  261. deserialize(doc.getElementsByTagName("Test").item(0));
  262. }
  263.  
  264. public void deserialize(Node myNode) throws ParseException {
  265. NodeList nodes;
  266. _item = new ArrayList<>();
  267. for (Node node : UtilityMethods.getNodesByTagName(myNode, "Item")) {
  268. Item item;
  269. item = new Item();
  270. item.deserialize(node);
  271. _item.add(item);
  272. }
  273. }
  274. }
Add Comment
Please, Sign In to add comment