Results 1 to 2 of 2

Thread: Parsed Document

  1. #1
    nick211001 is offline Member
    Join Date
    Jul 2007
    Posts
    1
    Rep Power
    0

    Default Parsed Document

    Hi,

    I have developed the following method which extracts data from a saxed XML document:


    public void startElement(String elementName, AttributeList al) throws SAXException
    {
    //Executed when a start element is encountered
    //elementName contains the name of the element and al contains
    //a list of the attributes
    String attributeName, attributeValue;
    if(al.getLength()>0)//iteration process through the parsed document
    for(int j = 0;j<al.getLength();j++)

    {
    attributeName = al.getName(j);
    attributeValue = al.getValue(j);
    {
    if (attributeName.equals("TYPE"))
    if (total.containsKey(attributeValue)) {
    Integer temp = (Integer)total.get(attributeValue);
    int temp2 = temp.intValue();
    total.put(attributeValue, new Integer(temp2+1));
    }
    else if (!total.containsKey(attributeValue))
    total.put(attributeValue, new Integer(1));
    }
    }
    Iterator i = total.entrySet().iterator();
    while (i.hasNext()) {
    Map.Entry temp = (Map.Entry)i.next();
    System.out.println(temp.getKey() + " " + temp.getValue());
    }


    This iterates through the parsed document and appears to run the method each time a "TYPE" (XML extract <FILE TYPE = "Word">) attribute is encounted.

    Can anyone tell me how to stop it continually iterating since I only need it to present the final totals ie. Word Files = 3, Excel Files = 2 etc.

  2. #2
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default You should try it like this

    If you implement the ContentHandler interface and use that with your sax parser it works a lot better. Then the following code will get you what you are looking for, I think.

    Java Code:
    public void startElement(String uri, String localName, String qName, Attributes attr) 
    		throws SAXException
    	{
    	String type = attr.getValue("TYPE");
    	if (type != null)
    		{
    		if (total.containsKey(type)) 
    			{
    			//Java 1.5 autoboxing will take care of this for you
    			int temp = (Integer)total.get(type);
    			total.put(type, new Integer(temp+1));
    			}
    		else if (!total.containsKey(type))
    			total.put(type, new Integer(1));
    		}
    	}
    	
    public void endDocument()
    	{
    	Iterator i = total.entrySet().iterator();
    	while (i.hasNext()) 
    		{
    		Map.Entry temp = (Map.Entry)i.next();
    		System.out.println(temp.getKey() + " " + temp.getValue());
    		}
    	}

Similar Threads

  1. Building a document from a DOM
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 10:22 AM
  2. Need help with Document interface
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 12-01-2007, 12:03 AM
  3. Personal Document Manager 0.6
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 08-11-2007, 11:43 PM
  4. add a xml document
    By Jack in forum XML
    Replies: 2
    Last Post: 07-04-2007, 10:21 AM
  5. load a document to the server
    By Heather in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-29-2007, 03:08 PM

Posting Permissions

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