I am trying to parse an XML document using SAX. The XML document looks like this:
& lt; Users & gt; & Lt; Line id = "- 1" DisplayName = "apple" & gt; & Lt; Line id = "1" DisplayName = "banana" & gt; & Lt; Line id = "2" DisplayName = "orange" & gt; & Lt; / Users & gt; This is a fraction of my parser class, which is responsible for parsing line elements: class SAXParser DefaultHandler Expands {static int i = 0; ArrayList & LT; ArrayList & LT; String & gt; & Gt; Ar = new array list & lieutenant; Arrest & lt; String & gt; & Gt; (); ArrayList & LT; String & gt; ID = new ArrayList & lt; String & gt; (); Public Zero Start Element (string yuri, string localname, string qName, attribute ett) {if (qName.equals ("row")) {int idx = 0; Id.add (idx, atts.getValue (0)); IDX ++; Id.add (idx, atts.getValue (3)); Ar.add (i, id); I ++; // idx = 0; }}} If I run a program, I get the following code in the ar attribute, which is not desired: [2, orange, 1, banana, -1], [2, orange, 1, banana, -1, app], [2, orange, 1, banana, -1], [2] orange , 1, banana, -1, apple]] What do I need id , DisplayName to add AR Such attribute: [- 1, Apple], [1, banana], [2, orange] What did I do wrong? How do I get the desired result?
The problem is that you id list only once, thus that list Will contain all the elements of all rows (identifiers and display names). You have to create a new ArrayList for each pair: class SAXParser DefaultHandler extended {static int = 0; ArrayList & LT; ArrayList & LT; String & gt; & Gt; Ar = new array list & lieutenant; Arrest & lt; String & gt; & Gt; (); // ***** Instead of making a list here ***** // Array List & lt; String & gt; ID = new ArrayList & lt; String & gt; (); Public Zero Start Element (String Yuri, string local-name, string qName, attribute ATT) {if (qName.equals ("line")) {// ***** Get that line here: ***** ArrayList & lt; String & gt; ID = new ArrayList & lt; String & gt; (); Int idx = 0; Id.add (idx, atts.getValue (0)); IDX ++; Id.add (idx, atts.getValue (3)); Ar.add (i, id); I ++; // idx = 0; }}} Note that if you add items to the indexing index, then you do not need to specify the index for each addition, the elements will be placed in the order that they are added I went . Then your code can be simplified:
public zero startArticles (string yuri, strings local-name, string qName, attribute ATT) {if (qName.equals ("row" )) {ArrayList & lt; String & gt; ID = new ArrayList & lt; String & gt; (); Id.add (atts.getValue (0)); Id.add (atts.getValue (3)); Ar.add (id); }}
Comments
Post a Comment