parsing attributes...please help!

admin

Administrator
Staff member
Hi!
I'm in a muddle trying to parse a collection of Attributes.
I can view them from within the parsing class, but I don't seem to be able to pass them out to an Attribute in the calling class, where I want to display it.
Is there something I'm missing about parsing attributes from child elements?

The XML document I'm parsing looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<!-- Configuration file for the servlet -->
<config>
<database>
<dbserver>brunnert:1433</dbserver>
<dblogin>repsuser</dblogin>
<dbpassword>repsuser</dbpassword>
<forms>
<form name="search_queryform"/> <form name="search_linkedtable" table="RepsBehalf"/>
</forms>
</database>
<style>
<errorstyle>../error.xsl</errorstyle>
</style>
</config>



I want to take the attributes of the <form> elements and store them into a Hashtable.
My parser looks like this:


public class MySAXParser extends DefaultHandler implements java.io.Serializable
{

private String text;
private Vector vector = new Vector();
private MySAXElement current = null;
private Attributes MyAttributes;
public static String s = new String();
public MySAXParser()
{}

public Vector parse(String filename) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); SAXParser saxParser = spf.newSAXParser(); // create an XML reader XMLReader reader = saxParser.getXMLReader(); FileReader in = new FileReader(filename); // set handler reader.setContentHandler(this); // call parse on an input source reader.parse(new InputSource(in)); return vector;

}


// receive notification of the beginning of an element

public void startElement (String uri, String name, String qName, Attributes atts) {

current = new MySAXElement(uri, name, qName, atts);
vector.addElement(current);
text = new String();
int len = atts.getLength();
MyAttributes = atts;

//this loop stores the attributes from the SAXElement into the a new Attribute object
//only goes into this loop if there is more than 1 attribute

for(int i = 0; i < len; i++){
s = MyAttributes.getValue(i);
//System.out.println("1. att val = " + s);
}

}

public String returnAttValue(){

//System.out.println("return Val: "+s); return s;
}

//I'd like this method to return the attributes to the calling class

public Attributes getAttributes(){ return MyAttributes;
}

// receive notification of the end of an element
public void endElement (String uri, String name, String qName) {

if(current != null && text != null)
{

current.setValue(text.trim());
}
current = null;
}


// receive notification of character data

public void characters (char ch[], int start, int length) { if(current != null && text != null) { String value = <!-- m --><a class="postlink" href="http://www.webdeveloper.com/forum/archive/index.php/new">http://www.webdeveloper.com/forum/archive/index.php/new</a><!-- m --> String(ch, start, length); text += value;
}
}
}


The class that I'm trying to retrieve the info from the parser looks like this:
(where FormsTable is the HashTable, tag is a string holding the current tag name and p is the parser object):




if(tag.equals("form"))
{
Attributes att = p.getAttributes();

for( int i = 0; i<att.getLength(); i++){ String aName = att.getLocalName(i);
if(aName == "form")
{
x = att.getValue(i);
}
if(aName == "table")
{
y = att.getValue(i);
}

//puts the key, and object into the Hashtable FormsTable.put(x,y);
}
}


It parses the rest of the XML doc fine, it just seems to run into difficulty with the attributes. The lengh of the attributes passed in is always equal to zero!

The key should be either 'name' or 'table' and the value whatever the value of that attribute. Any idears?
Any assistance and I'll be so greatful!
(Apologies for indentation, this is the best I could get it to)

Thanks!
 
Back
Top