• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

parsing XML-like string

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a string which looks like
<city><name>Bombay</name><xcoord>1234</xcoord><ycoord>5678</ycoord><size>1357</size><population>97531</population> </city>
how do I
1)get a list of tags in the string?
2)get the values within the tags?
using the org.apache.xerces.parsers.SaxParser
Thnx in advance
PS I am a novice(as you can see)
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you seen the SAXParser documentation? It provides an interface called ContentHandler which you can implement. The idea is, during the parsing the parser calls( "call-back" ) your code and so you can take any decisions you want to take, including extracting the content embedded between the tags.
So you will first create a ContentHandler implementation, code all the interface methods that you're interested in ( definitely the StartElement method ). Then you can call SetContentHandler on the parser passing your implementation as the argument. Then just start the parsing.
Good luck,
[PS: If you're just playing around, take a look at DOM parsing also. It is a lot neater than SAX parsing since it is not event-based ]
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to ask, what is "XML-like" ?? Just remember it has to be a well-formed XML document in order for the parser to even consider it for parsing.
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic