Error parsing xml using SAXParser

Lacienega

New Member
I have a problem when parsing xml from the internet. The parser doesn't return all the data correctly.Three are three errors:correct result -->return result161:1:161-->1:1:161330:2:132-->3:2:132421:2:223-->4:2:223Copy of the xml file I am trying to parsehttps://docs.google.com/open?id=0BwXEx9yI14inT1BnR2xzYnJEX0EActivity\[code\]public class DataBaseUpdateService_1 extends Activity { private TextView TextView1 ; private LinearLayout linearlayout1 ; private TextView title[]; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show_item); linearlayout1 = (LinearLayout)findViewById(R.id.linearlayout1); TextView1 = (TextView)findViewById(R.id.textView1); MyDBHelper dbHelper =new MyDBHelper(DataBaseUpdateService_1.this); SQLiteDatabase db = dbHelper.getWritableDatabase(); try { /** Handling XML */ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); /** Send URL to parse XML Tags */ URL sourceUrl = new URL( "http://123.com/example.xml"); /** Create handler to handle XML Tags ( extends DefaultHandler ) */ DataBaseUpdate_XMLHandler XMLHandler = new DataBaseUpdate_XMLHandler(); xr.setContentHandler(XMLHandler); xr.parse(new InputSource(sourceUrl.openStream())); }catch (Exception e) { System.out.println("XML Pasing Excpetion = " + e); } int itemCount = DataBaseUpdate_XMLHandler.array.size(); db.delete("hymns_match", null, null); try{ for(int i=0;i<itemCount;i++) { String songs_id=DataBaseUpdate_XMLHandler.array.get(i).get("songs_id"); String songs_book_id=DataBaseUpdate_XMLHandler.array.get(i).get("songs_book_id"); String songs_book_ch=DataBaseUpdate_XMLHandler.array.get(i).get("songs_book_ch"); TextView tv = new TextView(DataBaseUpdateService_1.this); tv.setText(songs_id + ":"+songs_book_id+ ":"+songs_book_ch); linearlayout1.addView(tv); } }catch (Exception e) { System.out.println("XML Pasing Excpetion = " + e); } }}\[/code\]DataBaseUpdate_XMLHandler\[code\]public class DataBaseUpdate_XMLHandler extends DefaultHandler { Boolean currentElement = false; String currentValue=http://stackoverflow.com/questions/10763864/null; static ArrayList<LinkedHashMap<String, String>> array; LinkedHashMap map; @Override public void startDocument() throws SAXException { array = new ArrayList<LinkedHashMap<String, String>>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { currentElement = true; if (localName.equals("song")) { map = new LinkedHashMap<String, Object>(); currentValue=http://stackoverflow.com/questions/10763864/null; } /*Get attribute * else if (localName.equals("website")) { * String attr = attributes.getValue("category"); * sitesList.setCategory(attr);} * */ } @Override public void endElement(String uri, String localName, String qName) throws SAXException { currentElement = false; /** set value */ if (localName.equalsIgnoreCase("songs_id")){ map.put("songs_id",currentValue);} else if (localName.equalsIgnoreCase("songs_book_id")){ map.put("songs_book_id", currentValue);} else if (localName.equalsIgnoreCase("songs_book_ch")){ map.put("songs_book_ch", currentValue);} else if (localName.equalsIgnoreCase("song")){ array.add(map);} } /** Called to get tag characters @Override public void characters(char[] ch, int start, int length) throws SAXException { if (currentElement) { currentValue = http://stackoverflow.com/questions/10763864/new String(ch, start, length); currentElement = false; } }}\[/code\]Can you give some advice about what's wrong here?
 
Back
Top