File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes getting child element name from sax ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "getting child element name from sax ?" Watch "getting child element name from sax ?" New topic

getting child element name from sax ?

kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
i am using jaxp in fallowing code i would like to know how we can get the child nodes ?

SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader();

xmlReader.setContentHandler(new MyContentHandler());

instream = new FileInputStream("books.xml");
is = new InputSource(instream);

i am getting all element names from
public void startElement(String namespaceURI, String localName,
String qName, Attributes attributes) {

if (localName.equals("book")) {
System.out.println( "\nBook Details");
int attrsLength = attributes.getLength();
for (int i = 0 ; i < attrsLength ; i ++ ) {
String itemId= attributes.getValue(i);
System.out.println("ItemId " + itemId);

how to get child node names specifically
i want name from office
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
In SAX programming it is up to your program to "remember" where it "is" by keeping some information. For example, you might set a flag when the name element starts and record characters in a StringBuffer until the element ends, then when you hit endElement for "office" the name data will be the content you want.
You will find yourself recording lots of data that is later discarded, but that is the price paid for the speed and low memory requirements of SAX processing.
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
thanks for the answer
I agree. Here's the link:
subject: getting child element name from sax ?
jQuery in Action, 3rd edition