java - SAXParser: handle only specific parent childs -
i have xml structure
<data> <id>id</id> <title>datatitle</title> <entry> <title>title1</title> </entry> <entry> <title>title2</title> </entry> </data>
and want parse , save in list title elements under entry. how can check in endelement title under entry? not have nullpointerexpception because parser tries save title data child.
@override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { elementon = true; if ("entry".equals(localname)) { song = new song(); } } @override public void endelement(string uri, string localname, string qname) throws saxexception { elementon = false; if ("title".equalsignorecase(localname)) { song.settitle(elementvalue); } else if ("entry".equalsignorecase(localname)) { songlist.add(song); } }
in case, can use stack push , pop cdata @ startelement , endelement events respectively. put check in endelement event data require stored
Comments
Post a Comment