Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java 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)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

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 us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-28-2007, 06:09 PM
Member
 
Join Date: Jul 2007
Posts: 1
nick211001 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-29-2007, 02:53 AM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
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.

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()); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Building a document from a DOM Java Tip Java Tips 0 01-03-2008 10:22 AM
Need help with Document interface cbalu AWT / Swing 1 12-01-2007 12:03 AM
Personal Document Manager 0.6 JavaBean Java Announcements 0 08-11-2007 11:43 PM
add a xml document Jack XML 2 07-04-2007 10:21 AM
load a document to the server Heather JavaServer Pages (JSP) and JSTL 2 06-29-2007 03:08 PM


All times are GMT +3. The time now is 05:00 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org