XML Parsing

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

XML Parsing

Postby Wubinator » Mon Apr 27, 2009 10:07 pm

Hi all,

I'm working on parsing the following XML http://api.eve-online.com/account/Chara ... 36GO2lvQNM

I retrieve the XML and I wrote a parser that extends from DefaultHandler. Now everything works fine except I want to get the complete contents of ever <row .... /> and set all the different elements in a model. But startElement(...) only goes through the tags not the complete contents of the tag.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CharacterSheetParser extends DefaultHandler {
  2.  
  3.         private String url;
  4.  
  5.         private StringBuilder text;
  6.  
  7.         private Characters characters = new Characters();
  8.  
  9.         private Character characterModel;
  10.  
  11.         private boolean fillingCharacter;
  12.  
  13.  
  14.  
  15.         public CharacterSheetParser(String url) {
  16.  
  17.                 this.url = url;
  18.  
  19.         }
  20.  
  21.  
  22.  
  23.         /**
  24.  
  25.          * Read the url and parse the XML
  26.  
  27.          *
  28.  
  29.          * @throws ParserConfigurationException
  30.  
  31.          * @throws SAXException
  32.  
  33.          * @throws IOException
  34.  
  35.          */
  36.  
  37.         public void parse() throws ParserConfigurationException, SAXException,
  38.  
  39.                         IOException {
  40.  
  41.  
  42.  
  43.                 InputStream urlInputStream = null;
  44.  
  45.                 SAXParserFactory spf = null;
  46.  
  47.                 SAXParser sp = null;
  48.  
  49.                 URL url = new URL(this.url);
  50.  
  51.                 urlInputStream = url.openConnection().getInputStream();
  52.  
  53.                 spf = SAXParserFactory.newInstance();
  54.  
  55.                 if (spf != null) {
  56.  
  57.                         sp = spf.newSAXParser();
  58.  
  59.                         sp.parse(urlInputStream, this);
  60.  
  61.                 }
  62.  
  63.                 if (urlInputStream != null)
  64.  
  65.                         urlInputStream.close();
  66.  
  67.         }
  68.  
  69.        
  70.  
  71.         public void startElement(String uri, String name, String qName,
  72.  
  73.                         Attributes atts) {
  74.  
  75.                
  76.  
  77.                 // When a result is found the
  78.  
  79.                 if (name.trim().equals("row")) {
  80.  
  81.                         this.characterModel = new Character();
  82.  
  83.                         this.fillingCharacter = true;
  84.  
  85.                 }
  86.  
  87.         }
  88.  
  89.        
  90.  
  91.         public void endElement(String uri, String localName, String qName) {
  92.  
  93.                 if (localName.equalsIgnoreCase("row")) {
  94.  
  95.                         this.characters.addCharacter(characterModel);
  96.  
  97.                         this.fillingCharacter = false;
  98.  
  99.                 }
  100.  
  101.         }
  102.  
  103. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


So what I want to do is get the "name" element inside the row-tag and set it in this.characterModel.setName(..) and so on.

How can I get to those elements?

Thans again,
Rick
Wubinator
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Apr 24, 2009 9:24 pm
Location: Amsterdam

Top

Postby padde » Tue Apr 28, 2009 12:13 am

There are so many topics regarding this... even a detailed tutorial.
So please use the search function... its the RED colored button with the magnifier icon
directly under the right foot of the big green android on the top of the site :evil:

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Wubinator » Tue Apr 28, 2009 7:07 am

I know where the search button is and I've used it, but apparently I don't search on the right keywords because I cannot find it.

Thanks for the response anyway
Wubinator
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Apr 24, 2009 9:24 pm
Location: Amsterdam

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests