java - Empty list in SAX parser endDocument after successfully parsing elements to the list -
I am trying to parse an XML containing geographical nodes and connecting nodes using the SAX parser methods of. I have a in Arreemap & lt; Long, MapPos & gt; and
ArrayList & lt; ArrayList & lt; MapPos & gt; & Gt; I archive parsed nodes in . When somehow parsed, I refer to the
ArrayList & lt; MapPos & gt; I and add it to the
ArrayList manner.
After debugging an application, I see that
startElement () and
endElement () for successfully
ArrayList Ways to add, but there is no bunch of empty code in
endDocument () method methods
ArrayList .
Here is the Java class:
Public Class ParkingDataCactactor {Private Static List & lt; Arrest & lt; MapPos & gt; & Gt; Roads = new arreelists & lt; ArrayList & lt; MapPos & gt; & Gt; (); Public static list & lt; ArrayList & lt; MapPos & gt; & Gt; GetWaysFromXML () throws pars configuration exception, SAXException, IOException {SAXParser saxParser = SAXParserFactory.newInstance (). NewSAXParser (); Default handler handler = new default handler () {arreemap & lt; Long, MapPos & gt; Nodes = new arreemap & lt; Long, MapPos & gt; (); ArrayList & LT; MapPos & gt; Nodbuffer = new arreelist & lt; MapPos & gt; (); & Lt; ArrayList & LT; MapPos & gt; & Gt; Ways = new arreelists & lt; ArrayList & lt; MapPos & gt; & Gt; (); // Private IT i; // For the purpose of debug @ Override, Public Own Start Begin (string Yuri, string localism, string QN, attribute attributes) throws SXExpress {if (qName.equalsIgnoreCase ("node")) {long id = long.parseLong () Attributes.getValue ("id")); Float lat = float. Pursefault (attributes.get value ("late")); Float lawn = float .Parceflot (attributes) ("lawn"); Nodes. Input (ID, new mappos (latitude, loan)); } And if (qName.equalsIgnoreCase ("nd")) {long referee = long.exe (attributes.getValue ("ref")); NodeBuffer.add (nodes.get (reference)); }} @ Override throws public zero end element (string yuri, string locale, string qName) SAXException {if (qName.equalsIgnoreCase ("route")) {ways.add (nodeBuffer); // i ++; // if (i == 15 9) // last element // array list & lt; MapPos & gt; Test = ways.get (i-1); // test = [MapPos [x = ..., y = ..., z = 0.0], MapPos [x = ..., y = ..., z = 0.0], ...] nodeBuffer.clear (); }} @ Override Public Wide and Document () throws SAXException {// Array List & lt; MapPos & gt; Test = ways.get (i-1); // test = [] roads = methods; }}; SaxParser.parse ("file: //" + Environment.getExternalStorage directory (+ + "/roadmap.xml", handler); Return roads; }}
when you call nodeBuffer.clear () You empty the list that you have passed in the way so far. You basically use the same nodebuffer object, and fill out the method list with multiple contexts of the same object - which you do every time you do below Are there.
Create the new by using the new ArrayList object as you should, and do it every time nodeBuffer Assign to After this you will have different objects, each will have a list of parsed nodes in the latest round.
Comments
Post a Comment