Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting child element name from sax ?

 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using jaxp in fallowing code i would like to know how we can get the child nodes ?

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
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")) {
authorCount=0;
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
<address>
<name></name>
<address>
<office>
<name></name>
</office>
i want name from office
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
kamesh aru
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the answer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic