Geeks Talk

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.

xml parsing.......

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.*; ...

Go Back   Geeks Talk > Web Development > AJAX & XML
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 06-05-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 984
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
Question 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.
Reply With Quote
Sponsored Links
Reply

  Geeks Talk > Web Development > AJAX & XML

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
parsing data henri084 Java 2 06-18-2009 12:19 PM
Parsing problem in Python pkamath Python 0 04-04-2009 12:42 PM


All times are GMT -4. The time now is 09:46 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved