Results 1 to 2 of 2
Thread: Parsed Document
- 07-28-2007, 05:09 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 1
- Rep Power
- 0
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.
- 07-29-2007, 01:53 AM #2
Senior Member
- Join Date
- Jul 2007
- Posts
- 135
- Rep Power
- 0
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
-
Building a document from a DOM
By Java Tip in forum Java TipReplies: 0Last Post: 01-03-2008, 09:22 AM -
Need help with Document interface
By cbalu in forum AWT / SwingReplies: 1Last Post: 11-30-2007, 11:03 PM -
Personal Document Manager 0.6
By JavaBean in forum Java SoftwareReplies: 0Last Post: 08-11-2007, 10:43 PM -
add a xml document
By Jack in forum XMLReplies: 2Last Post: 07-04-2007, 09:21 AM -
load a document to the server
By Heather in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 06-29-2007, 02:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks