Results 1 to 2 of 2

Thread: parsing attributes...please help!

  1. #1
    Join Date
    Feb 2003

    parsing attributes...please help!

    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 -->
    <form name="search_queryform"/>		      <form name="search_linkedtable" table="RepsBehalf"/>	

    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);	
    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 = null;	
    // receive notification of character data	
    public void characters (char ch[], int start, int length) {		if(current != null && text != null) {			String value = new 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):

              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)


  2. #2
    Join Date
    Dec 2002
    Calgary, Canada
    Since I do not have the complete parsing code infront of me I can only guess that you are not getting attributes correctly
    Here is an idea.

    first get a parent node that has child nodes with attributes

    pass that parent node to this method

    public void parseNodeElements(Node node){
    NodeList nl = node.getChildNodes();
    for(int x=0;x<nl.getLength();x++){
    Node el = nl.item(x);
    System.out.println(el.getNodeName()+" has no attributes");

    public void printAttributes(NamedNodeMap attribList){
    for(int x=0;x<attribList.getLength();x++){
    System.out.println("Attribute Name = "+attribList.item(x).getNodeName()+", atribute value = "+attribList.item(x).getNodeValue());

    and the method below will print the attribute names and values.



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center