Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 13th, 2003, 03:35 PM
Authorized User
 
Join Date: Oct 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to add all <price> element value?

I have a book.xml file, my xml file:-
======================================
<?xml version="1.0" ?>
<books>
<book category="reference">
<author>Kevin William</author>
<title>Cooking Lean</title>
<price>10.99</price>
</book>
<book category="fiction">
<author>Oprah</author>
<title>Star Track II</title>
<price>12.99</price>
</book>
<book category="fiction">
<author>Andy Koh</author>
<title>Basketball Freak</title>
<price>54.99</price>
</book>
<book category="reference">
<author>Irene Kim</author>
<title>Living Lean</title>
<price>50.99</price>
</book>
</books>
=============================

I want to find the average price of the books according to their category. For example, category "fiction" ... the average price will be ($12.99 + $54.99 ) / 2 ... how can I do that? I am using SAX with Java. My code as follow, but I found that my total price dind't accumulate ... :( so the average price always 0.0 ...

==============================


import org.xml.sax.Attributes;

final class Sink extends org.xml.sax.helpers.DefaultHandler implements org.xml.sax.ContentHandler
{
private int count = 0;
private int t = 0;
private int book = 0;
private int fiction = 0;
private int reference = 0;
public double AF = 0.0;
public double RF = 0.0;
public double averageF = 0.0;
public double averageR = 0.0;
public boolean isFiction = false;
public boolean isReference = false;
public StringBuffer content = new StringBuffer();

final private void print ( final String context, final String text )
{

System.out.println( context + ": " + text + "." );

t = count + 1;
count = t;

if (t % 4 == 0)
{
System.out.println("");
}
}

final private String firstPrint (final String text)
{
System.out.print(text);
return text;
}


final public void startElement ( final String namespace, final String localname, final String type, final org.xml.sax.Attributes attributes ) throws org.xml.sax.SAXException
{
if (type.equals("book"))
{
book++;

String category = attributes.getValue("category");
isFiction = category.equals("fiction");
isReference = category.equals("reference");

if (isFiction) {
fiction++;
print("Category", category);
}

if (isReference) {
reference++;
print("Category", category);
}

content.setLength(0);
}


if (type == "author")
{
firstPrint ("Author");
}

if (type == "title")
{
firstPrint ("Title");
}

if (type == "price")
{
firstPrint ("Price");
}

}

final public void endElement (final String name) throws org.xml.sax.SAXException
{
if ((name.equals("price")) && isFiction)
{
double price = new Double(content.toString()).doubleValue();
averageF = averageF + price;
// content.setLength(0);
}

if ((name.equals("price")) && isReference)
{
final double price2 = new Double(content.toString()).doubleValue();
averageR = averageR + price2;
//content.setLength(0);
}
}

final public void endDocument() throws org.xml.sax.SAXException
{
AF = averageF / fiction;
RF = averageR / reference;
totalBooks(book, fiction, reference, AF, RF);
}

final public void characters ( final char[] ch, final int start, final int len )
{

content.append(ch, start, len);
final String text = new String( ch, start, len );
final String text1 = text.trim();

if( text1.length() > 0 ) {
print("", text1);
}
}

final private void totalBooks(final int num, final int fic, final int ref, final double aFic, final double aRef)
{

System.out.println("There are total " + num + " books in the XML file.");
System.out.println("There are " + fic + " FICTION books in the XML file. Average price is: $" + aFic);
System.out.println("There are " + ref + " REFERENCE books in the XML file. Average price is: $" + aRef);
}

}
 
Old November 14th, 2003, 03:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Actually you didn't override the ContentHandler's endElement method: you added another method which takes one String argument. ContentHandler's endElement method has the signature:
Code:
endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
That's why you can't handle this event (and hence you can't accumulate the prices).

Regards,
Armen
 
Old November 14th, 2003, 10:51 AM
Authorized User
 
Join Date: Oct 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot, Armen. It works now. However, I am confused about something ... please guide me ...

what is the diff between using "java.lang.String qName" and "final String qName" ? I changed it to "final String qName" and it didn't work. But my startElement( final String namespace, final String localname, final String type, final org.xml.sax.Attributes attributes ) works fine.

Must we include (java.lang.String uri, java.lang.String localName, java.lang.String qName) in endElement everytime? What does uri, localName and qName means?

 
Old November 14th, 2003, 11:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:
what is the diff between using "java.lang.String qName" and "final String qName" ? I changed it to "final String qName" and it didn't work. But my startElement( final String namespace, final String localname, final String type, final org.xml.sax.Attributes attributes ) works fine.
When you specify the "final" modifier, you can't assign to that argument. Since SAX is a read-only API, you needn't use "final" here, so drop all "final" argument-modifiers in your method signatures.

Quote:
quote:
Must we include (java.lang.String uri, java.lang.String localName, java.lang.String qName) in endElement everytime? What does uri, localName and qName means?
Yes, always; otherwise you will not be able to handle the events! You must override(implement) exactly the method which is described in the API.
uri - is the namespace URI for the current element(e.g., it is "http://www.w3.org/1999/XSL/Transform" for XSLT elements)
localName - local part of QName describing the element type of the current element (e.g., if element type is "xsl:stylesheet" then the local name is "stylesheet")
qName - as described just above

Regards,
Armen
 
Old November 14th, 2003, 05:52 PM
Authorized User
 
Join Date: Oct 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot, Armen :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
remove <div>-element Kabe Classic ASP Professional 0 November 9th, 2007 05:41 AM
create <ul> with alternating class on <li> element Brian Campbell XSLT 2 November 3rd, 2006 06:07 PM
name attribute for <xsl:element> rushman XSLT 2 June 9th, 2005 09:04 AM
literal result element <head> pietdemeester BOOK: XSLT Programmer's Reference, 2nd Edition 0 June 21st, 2003 08:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.