Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. <channel>
  2. <item>
  3. <name>Hello Earth</name>
  4. <place>Earth</place>
  5. </item>
  6. <item>
  7. <name>Goodbye Mars</name>
  8. <place>Mars</place>
  9. </item/>
  10. </channel>
  11.  
  12. AssetManager assetManager = getAssets();
  13. InputStream inputStream = assetManager.open("words.xml");
  14.  
  15. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  16. factory.setCoalescing(true);
  17. DocumentBuilder builder = factory.newDocumentBuilder();
  18.  
  19. Document dom = builder.parse(inputStream);
  20.  
  21. org.w3c.dom.Element root = dom.getDocumentElement();
  22. NodeList items = root.getElementsByTagName("item");
  23.  
  24. String name="";
  25. String place="";
  26.  
  27. for (int i = 0; i < items.getLength(); i++) {
  28. Node item = items.item(i);
  29. NodeList properties = item.getChildNodes();
  30.  
  31. for (int j = 0; j < properties.getLength(); j++) {
  32. Node property = properties.item(j);
  33. String propertyName = property.getNodeName();
  34.  
  35. if (propertyName.equalsIgnoreCase("name")) {
  36. String strText = property.getFirstChild()
  37. .getNodeValue();
  38. nam = strText;
  39. }
  40.  
  41. if (propertyName.equalsIgnoreCase("place")) {
  42. String strText = property.getFirstChild()
  43. .getNodeValue();
  44. place = strText;
  45. }
  46.  
  47. db.insertWord(name, place);
  48. }
  49.  
  50. <table name="item">
  51. <column name="name">Hello Earth</column>
  52. <column name="place">Earth</column>
  53. </table>
  54. <table name="item">
  55. <column name="name">Goodbye Mars</column>
  56. <column name="place">Mars</column>
  57. </table>
  58.  
  59. class Item
  60. {
  61. String name;
  62. String place;
  63. }
  64.  
  65.  
  66.  
  67. ArrayList<Item>items=new ArrayList<Item>();
  68.  
  69.  
  70.  
  71.  
  72. void somemethod()
  73. {
  74.  
  75. AssetManager assetManager = getAssets();
  76. InputStream inputStream = assetManager.open("words.xml");
  77.  
  78. Item item=new Item();
  79. try
  80. {
  81. XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  82. factory.setNamespaceAware(true);
  83. XmlPullParser xpp = factory.newPullParser();
  84. xpp.setInput(inputStream,null);
  85. int eventType = xpp.getEventType();
  86. while (eventType != XmlPullParser.END_DOCUMENT)
  87. {
  88. if(eventType == XmlPullParser.START_DOCUMENT)
  89. { }
  90. else if(eventType == XmlPullParser.START_TAG)
  91. {
  92. try
  93. {
  94. if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("name"))
  95. {
  96. eventType = xpp.next();
  97. item.name=Integer.parseInt(xpp.getText().toString());
  98. }
  99. else if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("place"))
  100. {
  101. eventType = xpp.next();
  102. item.place=xpp.getText().toString();
  103. }
  104. }
  105. catch (Exception e)
  106. {
  107. //e.printStackTrace();
  108. }
  109. }
  110. else if(eventType == XmlPullParser.END_TAG)
  111. {
  112. if(xpp.getName()!=null&& xpp.getName().equalsIgnoreCase("content"))
  113. {
  114. eventType = xpp.next();
  115. items.put(item);
  116. item=new Item();
  117. }
  118. }
  119. else if(eventType == XmlPullParser.TEXT)
  120. {}
  121. eventType = xpp.next();
  122. }// end of while
  123.  
  124. }
  125. catch (XmlPullParserException e)
  126. {
  127. //e.printStackTrace();
  128. }
  129. catch (IOException e)
  130. {
  131. //e.printStackTrace();
  132. }
  133. finally
  134. {
  135. try
  136. {
  137. if(inputStream!=null)
  138. inputStream.close();
  139. } catch (IOException e)
  140. {
  141. }
  142. }
  143.  
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement