aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "getting child element name from sax ?" Watch "getting child element name from sax ?" New topic
Author

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();
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

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Mar 16, 2002
Posts: 150
thanks for the answer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting child element name from sax ?