1. Are you ready for the Galaxy S20? Here is everything we know so far!

Xml Parser and Best Practice

Discussion in 'Android Development' started by KarneeKarnay, Jan 11, 2016.

  1. KarneeKarnay

    KarneeKarnay Newbie
    Thread Starter

    Hi Guys,

    I'm a relative novice to Android development. I'm trying to build a simple app that parses xml content from an online database and presents it in a list view to the user. My code has hit a snag though and only gathers the first two results. My code is below.

    Code (Text):
    1.  
    2.  
    3. import android.os.AsyncTask;
    4. import android.util.Log;
    5.  
    6. import org.w3c.dom.Document;
    7. import org.w3c.dom.NodeList;
    8. import org.xml.sax.InputSource;
    9.  
    10. import java.net.URL;
    11.  
    12. import javax.xml.parsers.DocumentBuilder;
    13. import javax.xml.parsers.DocumentBuilderFactory;
    14.  
    15. import org.w3c.dom.CharacterData;
    16. import org.w3c.dom.Element;
    17. import org.w3c.dom.Node;
    18.  
    19. public class Downloader extends AsyncTask <String,Void,Void> {
    20.  
    21.     NodeList nodelist;
    22.     NodeList gameList;
    23.  
    24.     protected void onPreExecture()
    25.     {
    26.         System.out.println("THis is doing something");
    27.     }
    28.  
    29.     protected  Void doInBackground(String... Url)
    30.     {
    31.         try
    32.         {
    33.             NodeFinder nodeSearch;
    34.             URL url = new URL(Url[0]);
    35.             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    36.             DocumentBuilder db = dbf.newDocumentBuilder();
    37.             //Download the XML File
    38.             Document doc = db.parse(new InputSource(url.openStream()));
    39.             doc.getDocumentElement().normalize();
    40.             //Locate the Tag Name
    41.             nodelist = doc.getElementsByTagName("Game");
    42.  
    43.             for (int i = 0; i < nodelist.getLength(); i++)
    44.             {
    45.                 nodeSearch = new NodeFinder(nodelist,i,"GameTitle");
    46.                 nodeSearch = new NodeFinder(nodelist,i,"Overview");
    47.             }
    48.         }
    49.         catch (Exception e)
    50.         {
    51.             Log.e("Error", e.getMessage());
    52.             e.printStackTrace();
    53.         }
    54.         System.out.println("This is going on in the  background");
    55.         return null;
    56.     }
    57.  
    58.     protected void onPostExecute(String args)
    59.     {
    60.         System.out.println("This has been executed!");
    61.     }
    62.  
    63. }
    NodeFinder - This class is looped through repeatedly until the for loop is finished. Am I breaking any rules in using a constructor instead of a method?

    Code (Text):
    1.  
    2. public class NodeFinder {
    3.  
    4.     public NodeFinder(NodeList gameHolder, int index, String nodeTag)
    5.     {
    6.         Element element = (Element) gameHolder.item(index);
    7.  
    8.         NodeList name = element.getElementsByTagName(nodeTag);
    9.         Element line = (Element) name.item(0);
    10.         System.out.println(nodeTag + " " + line.getChildNodes().item(0).getNodeValue());
    11.     }
    12. }
    13.  
    I've never attempted to use an xml parser before. I'm not sure what best practice is. Are there any issues you guys can see with my code in general and with only the first two elements being printed?

    Below is the XML Data and after that the log.

    Code (Text):
    1.  
    2. <Data>
    3. <baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl>
    4. <Game>
    5. <id>3597</id>
    6. <GameTitle>Halo 4</GameTitle>
    7. <PlatformId>15</PlatformId>
    8. <Platform>Microsoft Xbox 360</Platform>
    9. <ReleaseDate>11/06/2012</ReleaseDate>
    10. <Overview>...</Overview>
    11. <ESRB>M - Mature</ESRB>
    12. <Genres>...</Genres>
    13. <Players>2</Players>
    14. <Co-op>Yes</Co-op>
    15. <Youtube>http://www.youtube.com/watch?v=oBk3c4UIcGw</Youtube>
    16. <Publisher>Microsoft</Publisher>
    17. <Developer>343 Industries</Developer>
    18. <Rating>8.25</Rating>
    19. <Images>...</Images>
    20. </Game>
    21. <Game>
    22. <id>9</id>
    23. <GameTitle>Halo 2</GameTitle>
    24. <PlatformId>1</PlatformId>
    25. <Platform>PC</Platform>
    26. <ReleaseDate>05/31/2007</ReleaseDate>
    27. <Overview>
    28. Halo 2 is the sequel to the highly successful and critically acclaimed Halo®: Combat Evolved. In Halo 2, the saga continues as Master Chief—a genetically enhanced super-soldier—is the only thing standing between the relentless Covenant and the destruction of all humankind.
    29. </Overview>
    30. <ESRB>M - Mature</ESRB>
    31. <Genres>
    32. <genre>Action</genre>
    33. <genre>Shooter</genre>
    34. </Genres>
    35. <Players>2</Players>
    36. <Co-op>No</Co-op>
    37. <Youtube>http://www.youtube.com/watch?v=Zz6FNKawJBc</Youtube>
    38. <Publisher>Microsoft</Publisher>
    39. <Developer>Bungie</Developer>
    40. <Rating>7.25</Rating>
    41. <Similar>
    42. <SimilarCount>1</SimilarCount>
    43. <Game>
    44. <id>6079</id>
    45. <PlatformId>14</PlatformId>
    46. </Game>
    47. </Similar>
    48. <Images>
    49. <fanart>
    50. <original width="1280" height="720">fanart/original/9-1.jpg</original>
    51. <thumb>fanart/thumb/9-1.jpg</thumb>
    52. </fanart>
    53. <fanart>
    54. <original width="1280" height="720">fanart/original/9-2.jpg</original>
    55. <thumb>fanart/thumb/9-2.jpg</thumb>
    56. </fanart>
    57. <fanart>
    58. <original width="1920" height="1080">fanart/original/9-3.jpg</original>
    59. <thumb>fanart/thumb/9-3.jpg</thumb>
    60. </fanart>
    61. <boxart side="back" width="1520" height="2156" thumb="boxart/thumb/original/back/9-1.jpg">boxart/original/back/9-1.jpg</boxart>
    62. <boxart side="front" width="1527" height="2156" thumb="boxart/thumb/original/front/9-2.jpg">boxart/original/front/9-2.jpg</boxart>
    63. </Images>
    64. </Game>
    65. <Game>
    66. <id>90</id>
    67. <GameTitle>Halo 3</GameTitle>
    68. <PlatformId>15</PlatformId>
    69. <Platform>Microsoft Xbox 360</Platform>
    70. <ReleaseDate>09/25/2007</ReleaseDate>
    71. <Overview>
    72. Master Chief returns to a Covenant Dominated Earth on a mission to kill the final alien leader. Meanwhile, the Arbiter, Johnson, and Keyes form a loose alliance and escape from Delta Halo. The Covenant is ripped in civil war, and the Elites along with a handful of other alien races become sympathetic to the human cause. Delta Halo's impromptu dis-activation has brought all of the Halos to a "remote activation phase". They can be activated from a facility called the ark, which happens to be on Earth.
    73. </Overview>
    74. <ESRB>M - Mature</ESRB>
    75. <Genres>
    76. <genre>Shooter</genre>
    77. </Genres>
    78. <Players>2</Players>
    79. <Co-op>Yes</Co-op>
    80. <Publisher>Microsoft Game Studios</Publisher>
    81. <Developer>Bungie</Developer>
    82. <Rating>7.375</Rating>
    83. <Images>
    84. <fanart>
    85. <original width="1920" height="1080">fanart/original/90-1.jpg</original>
    86. <thumb>fanart/thumb/90-1.jpg</thumb>
    87. </fanart>
    88. <fanart>
    89. <original width="1920" height="1080">fanart/original/90-2.jpg</original>
    90. <thumb>fanart/thumb/90-2.jpg</thumb>
    91. </fanart>
    92. <fanart>
    93. <original width="1920" height="1080">fanart/original/90-3.jpg</original>
    94. <thumb>fanart/thumb/90-3.jpg</thumb>
    95. </fanart>
    96. <fanart>
    97. <original width="1920" height="1080">fanart/original/90-4.jpg</original>
    98. <thumb>fanart/thumb/90-4.jpg</thumb>
    99. </fanart>
    100. <boxart side="back" width="1518" height="2148" thumb="boxart/thumb/original/back/90-1.jpg">boxart/original/back/90-1.jpg</boxart>
    101. <boxart side="front" width="1530" height="2148" thumb="boxart/thumb/original/front/90-1.jpg">boxart/original/front/90-1.jpg</boxart>
    102. <clearlogo width="400" height="300">clearlogo/90.png</clearlogo>
    103. </Images>
    104. </Game>
    105.  
    Code (Text):
    1.  
    2. 01-11 23:22:17.654 420-447/com.example.karnee.myapplication E/Error: Attempt to invoke interface method 'org.w3c.dom.NodeList org.w3c.dom.Element.getChildNodes()' on a null object reference
    3. 01-11 23:22:17.654 420-447/com.example.karnee.myapplication W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'org.w3c.dom.NodeList org.w3c.dom.Element.getChildNodes()' on a null object reference
    4. 01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err:     at com.example.karnee.myapplication.NodeFinder.<init>(NodeFinder.java:17)
    5. 01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err:     at com.example.karnee.myapplication.Downloader.doInBackground(Downloader.java:48)
    6. 01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err:     at com.example.karnee.myapplication.Downloader.doInBackground(Downloader.java:22)
    7. 01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
    8. 01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    9. 01-11 23:22:17.655 420-447/com.example.karnee.myapplication W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    10. 01-11 23:22:17.656 420-447/com.example.karnee.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    11. 01-11 23:22:17.656 420-447/com.example.karnee.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    12. 01-11 23:22:17.656 420-447/com.example.karnee.myapplication W/System.err:     at java.lang.Thread.run(Thread.java:818)
    13. 01-11 23:22:17.656 420-447/com.example.karnee.myapplication I/System.out: This is going on in the  background
    14.  
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    Well, aside from the questionable code design, this is your problem. The structure below doesn't contain a <GameTitle> element, so you get a null pointer exception.

    Code (Text):
    1. <Game>
    2.   <id>6079</id>
    3.   <PlatformId>14</PlatformId>
    4. </Game>
    If elements are optional, then your code should be checking for nulls. Something like this:

    Code (Text):
    1. public class NodeFinder {
    2.     public NodeFinder(NodeList gameHolder, int index, String nodeTag)
    3.     {
    4.         Element element = (Element) gameHolder.item(index);
    5.         NodeList name = element.getElementsByTagName(nodeTag);
    6.         Element line = (Element) name.item(0);
    7.         if (line != null)
    8.         {
    9.           System.out.println(nodeTag + " " + line.getChildNodes().item(0).getNodeValue());
    10.        }
    11.     }
    12. }
     
    scary alien likes this.
  3. Deleted User

    Deleted User Guest

    scary alien likes this.
Loading...
Similar Threads - Xml Parser Best
  1. OfficeChamp
    Replies:
    3
    Views:
    459
  2. Biffos
    Replies:
    10
    Views:
    486
  3. AppleUser
    Replies:
    1
    Views:
    574
  4. QuantumGnosis
    Replies:
    2
    Views:
    1,160
  5. ozth89
    Replies:
    3
    Views:
    6,440
  6. brentc
    Replies:
    1
    Views:
    2,007
  7. FlipperTime
    Replies:
    0
    Views:
    1,363
  8. FlipperTime
    Replies:
    0
    Views:
    1,321
  9. batboy
    Replies:
    7
    Views:
    7,327
  10. Thomas Thorton
    Replies:
    0
    Views:
    1,893

Share This Page

Loading...