This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply 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: 12268
    
    1
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


Java Resources at www.wbrogden.com
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
thanks for the answer
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: getting child element name from sax ?
 
Similar Threads
Print out values of xml tags
DOM XML Parsing - Newbie
problem in getting xpath of each node.
Simple Name & Qualified Name
Referencing an array via a method.