| Author |
Getting Node Value using SAX
|
Graham Thorpe
Ranch Hand
Joined: Mar 25, 2002
Posts: 264
|
|
Hi Can any body tell us how to get the node value using SAX. I have one xml as follows <One> <TABLE name="dress"> <TR> <TD name="id">18</TD> <TD name="number">3</TD> </TR> </TABLE> </One> Can any body tell us where i am wrong with this code for geting the element node values (<TD> like 18 and 3 for the particular node <one>? import java.util.*; import java.io.*; import org.xml.sax.*; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import java.text.DateFormat; import java.text.SimpleDateFormat; public class SaxReader implements ContentHandler { boolean flag = false; StringBuffer sbf = new StringBuffer(); /** * Field tagName */ private String tagName_; /** * Field tagValue */ private StringBuffer tagValue_; /** * Field loanPurpose */ private String loanPurpose; /** * Field ErrorList */ private ArrayList ErrorList = new ArrayList(); /** * Field Data */ private HashMap Data = new HashMap(); /** * Constructor SAXDataSource * @param xml java.io.Reader * @throws IOException */ public SaxReader(Reader xml) throws IOException { try { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setValidating(true); SAXParser sp = saxParserFactory.newSAXParser(); ErrorHandler handler = new ErrorHandler() { public void warning(SAXParseException e) throws SAXException { ErrorList.add(e.getMessage()); } public void error(SAXParseException e) throws SAXException { ErrorList.add(e.getMessage()); } public void fatalError(SAXParseException e) throws SAXException { ErrorList.add(e.getMessage()); throw e; } }; XMLReader parser = sp.getXMLReader(); parser.setContentHandler(this); parser.setErrorHandler(handler); parser.parse(new InputSource(xml)); } catch (IOException e) { throw e; } catch (SAXException e) { e.getMessage(); } catch (Throwable x) { x.printStackTrace(); } } public void startElement(String namespaceURI, String localName,String qName, Attributes atts) { // Get the number of attribute int length = atts.getLength(); System.out.println("length.."+length + "localname"+ localName); // Process each attribute for (int i=0; i<length; i++) { // Get names and values for each attribute String name = atts.getQName(i); String value = atts.getValue(i); System.out.println("name=="+name+">>"+value); System.out.println("loop qname values.."+qName); System.out.println("qName.."+qName +"sds" + atts.getValue(namespaceURI,qName)); // The following methods are valid only if the parser is namespace-aware } } /** * Method endElement */ int count=0; public void endElement(String nsURI, String localName, String qName) throws SAXException { if(count >0) { count=0; } if (qName.equals("TABLE")) { flag = false; sbf.append("</" + qName + ">"); System.out.println(sbf.toString()); count=1; sbf=new StringBuffer(); } if (flag) { sbf.append("</" + qName + ">"); } } /** * Field echo */ private boolean echo = false; /** * Method characters This method is used to obtain the value for required Tags. * * @param chars char[] * @param pos * @param len * @exception SAXException */ public void characters(char[] chars, int pos, int len) throws SAXException { if (tagName_ != null && tagValue_ != null) { String value = new String(chars, pos, len); System.out.println("this is new...."+ value); tagValue_.append(value); Data.put(tagName_, tagValue_); tagName_ = null; tagValue_ = null; } if (flag) { String value = new String(chars, pos, len); sbf.append(value.trim()); } } /** * Method getData This method returns the * @return HashMap */ public HashMap getData() { return this.Data; } public void endDocument() throws SAXException { } public void endPrefixMapping(String arg1) throws SAXException { } public void ignorableWhitespace(char[] chars, int pos, int len) throws SAXException { } public void processingInstruction(String arg1, String arg2) throws SAXException { } public void setDocumentLocator(Locator arg1) { } public void skippedEntity(String arg1) throws SAXException { } public void startDocument() throws SAXException { } public void startPrefixMapping(String arg1, String arg2) throws SAXException { } /** * Method main for Testing * */ public static void main(String[] args) { try { SaxReader ds = new SaxReader(new FileReader("one.xml")); } catch (Throwable x) { x.printStackTrace(); } } } With the above i am getting output like this name id ----- name number Regards Reddy
|
 |
 |
|
|
subject: Getting Node Value using SAX
|
|
|