Hunter的大杂烩 技术学习笔记

2007-05-25

html parser使用范例

Filed under: Java — hunter @ 4:03 pm

 

package org.htmlparser;

import org.htmlparser.beans.StringBean;
import java.util.regex.*;
import java.util.Formatter;
import java.util.Locale;
import java.lang.String;
import java.util.Calendar;

public class StockMonitor2
{

    public static void main (String[] args)
    {
        if (args.length < 3 )
        {
            System.out.println (“Syntax : java -classpath ../lib/htmlparser.jar id maxprice min”);
        }
        else
        {
            try
            {
                Integer stock_id = new Integer(args[0]);
                Float   stock_price = new Float(args[1]);
                Float   stock_price2 = new Float(args[2]);
               
                StringBean se;
                se = new StringBean();
                se.setLinks(false);
               
                //StringBuilder sb = new StringBuilder();
                //formatter.format(“%4$2s %3$2s %2$2s %1$2s”, “a”, “b”, “c”, “d”)

                Locale l = null;
                String sUrl = String.format(l, “http://www.aastocks.com/eng/StockQuote/default.asp?symbol=%04d”,stock_id.intValue());
                se.setURL(sUrl);
                //System.out.println(se.getStrings());
                Pattern pattern = Pattern.compile(“Bid[\r\n \t]+([0-9.]+)”);
                Matcher matcher = pattern.matcher(se.getStrings());
                //Calendar rightNow = Calendar.getInstance();

                //System.out.println(“monitor time: ” + rightNow.get(Calendar.HOUR_OF_DAY) + “:” +
                //                    rightNow.get(Calendar.MINUTE) + “:” +rightNow.get(Calendar.SECOND));
                if (matcher.find())
                {
                    StringBuffer sBuffer = new StringBuffer(matcher.group());
                    Float now_price = new Float(sBuffer.substring(4));
                    if (now_price.compareTo(stock_price)>0 || now_price.compareTo(stock_price2)<0)
                    {
                        System.out.println(“

“+stock_id+” “+stock_price+” “+stock_price2+” “+now_price+” “+stock_id+” “+stock_price+” “+stock_price2+” “+now_price+”

“);
                    }
                }
                else
                {
                    System.err.println(“can’t match data:\n” + se.getStrings());
                }
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
            }
        }
    }
}

2007-05-20

httpclient & jericho xml范例

Filed under: Java — hunter @ 11:50 pm

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.lang.*;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import au.id.jericho.lib.html.Element;
import au.id.jericho.lib.html.HTMLElementName;
import au.id.jericho.lib.html.Segment;
import au.id.jericho.lib.html.Source;

public class HkStockInfo implements StockInfo
{
 private HttpClient httpClient = null;
 public HkStockInfo()
 {
  //构造HttpClient的实例
  httpClient = new HttpClient();
 }

(more…)

xml 读写范例

Filed under: Java — hunter @ 11:49 pm

import java.io.*;
import java.lang.Integer;
import java.util.*;
import java.lang.Math;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;

public class htmlparsertest
{
 /**
  *
  * @param doc
  * @param i
  * @param Hcode
  *
  * @return String
  */
 public static String getXmlValue(NodeList nl, int offset, String codename)
 {

(more…)

Powered by WordPress