Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on XML parsing within the AJAX & XML forums, part of the Web Development category; Hi guys, I have the code for parsing of xml complete tags but i need to parse also the empty tags. The code which i have is : import java.lang.reflect.*; ...
|
|||||||
|
|||
|
XML parsing
Hi guys,
I have the code for parsing of xml complete tags but i need to parse also the empty tags. The code which i have is : import java.lang.reflect.*; import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import java.util.*; public abstract class XMLParser extends DefaultHandler implements PercLogger { public String value = ""; public Vector tagStack = new Vector(); // we'll use this like a stack // to accomodate nested tags public Hashtable tagStarters = new Hashtable(); public Hashtable tagEnders = new Hashtable(); public PercList tagTriggers = new PercList(); public String parsestring = ""; public PercBasicLogger logger = null; public Object currentObject = null; // This method is only necessary for testing. You can save the output // of the CORBA payload string into a file and rerun your parser until // you get it write using this method, then at run time use the // method that takes a String public XMLParser(File inputfile, PercBasicLogger l) throws PercException { StringBuffer input = new StringBuffer(); try { FileInputStream io = new FileInputStream(inputfile); InputStreamReader reader = new InputStreamReader(io); char n[] = new char[io.available()]; reader.read(n); input.append(n); reader.close(); parsestring = input.toString(); logger = l; setupTags(); } catch (IOException ex) { throw new PercException("Can't read file: " + inputfile); } } public XMLParser(String inputstream, PercBasicLogger l) throws PercException{ parsestring = inputstream.replace('&', ' '); logger = l; setupTags(); } public void parse() throws PercException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); ByteArrayInputStream input = new ByteArrayInputStream(parsestring.getBytes()); try { SAXParser saxParser = factory.newSAXParser(); saxParser.parse(input, this); } catch (Throwable t) { t.printStackTrace(); throw new PercException(t.getMessage()); } } // any tag setup public abstract void setupTags(); public void startDocument() throws SAXException { debug("start document"); } public void endDocument() throws SAXException { debug("end document"); } public void characters(char buf[], int offset, int len) throws SAXException { value = new String(buf, offset, len); } public void push(String tag) { tagStack.add(tag); } public void pop() { if (tagStack.size() > 0) tagStack.remove(tagStack.lastElement()); } public String currentTag() { if (tagStack.size() == 0) return ""; return (String)tagStack.lastElement(); } public String completeTag() { if (tagStack.size() == 0) return ""; StringBuffer buf = new StringBuffer(); buf.append((String)tagStack.get(0)); for (int i = 1; i < tagStack.size(); i++) buf.append("." + (String)tagStack.get(i)); return buf.toString(); } public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException { push(qName); try { Method start = (Method)tagStarters.get(completeTag()); if (start != null) { Object params[] = {}; start.invoke(this, params); } } catch (Exception ex) { error(ex); } } public void endElement(String namespaceURI, String sName, String qName) throws SAXException { try { // we run the setter for the appropriate tag on the current object String key = completeTag(); XMLTrigger trigger = (XMLTrigger)tagTriggers.getValue(key); if (trigger != null) { Method setter = trigger.getTheMethod(); if (setter != null) { // We can check the setter type here and cast // value into the appropriate object/scalar type // right now we just pass it as its default type, String Object params[] = {value}; debug("Setting Field " + completeTag() + "=" + value); setter.invoke(currentObject, params); // clear value after setting value = ""; } else { debug("Setter Not Found: " + completeTag() + "=" + value); } } else { debug("Trigger Not Found: " + completeTag() + "=" + value); } Method end = (Method)tagEnders.get(completeTag()); if (end != null) { Object params[] = {}; end.invoke(this, params); } pop(); } catch (Exception ex) { error("Failed on Object " + currentObject.getClass().getName() + ": " + completeTag() + ": " + ex); } } public void debug(Object message) { logger.debug(this.getClass(), message); } public void error(Object message) { logger.error(this.getClass(), message); } public void fatal(Object message) { logger.fatal(this.getClass(), message); } public void info(Object message) { logger.info(this.getClass(), message); } public void warn(Object message) { logger.warn(this.getClass(), message); } } Please help me how can I add the code for doing the parsing of XML Empty Tags. Thanks in advance... Riju |
| Sponsored Links |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| parsing data | henri084 | Java | 2 | 06-18-2009 12:19 PM |
| xml parsing....... | rijus | AJAX & XML | 0 | 06-05-2009 06:46 AM |
| Parsing problem in Python | pkamath | Python | 0 | 04-04-2009 12:42 PM |