search.java

来自「这是一个用jsp+Oracle开发的联系人客户关系管理系统!」· Java 代码 · 共 49 行

JAVA
49
字号
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

public class search extends HandlerBase
{
	private double totalPrice = 0.0;
	private double averagePrice = 0.0;
	private int count = 0;
	static private Writer out;

	public static void main(String argv[]) throws Exception
	{
		/*if(argv.length!=1)
		{
			System.err.println("Usage: cmd filename");
			System.exit(1);
		}*/
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try
		{
			out = new OutputStreamWriter(System.out,"UTF-8");
			SAXParser saxParser = factory.newSAXParser();
			saxParser.parse(new File("money.xml"),new search());
		}
		catch(Throwable t)
		{
			t.printStackTrace();
		}
	}

	public void startElement(String name,AttributeList attrs) throws SAXException
	{
		if(name.equals("month"))
		{
			count = count + 1;
			double price = new Double(attrs.getValue("price")).doubleValue();
			totalPrice += price;
		}
	}

	public void endDocument()
	{
		averagePrice = totalPrice/count;
		System.out.println("the average money every month is "+averagePrice);
	}
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?