SAX解析实例 (二)

2014-11-24 07:34:29 · 作者: · 浏览: 4
else if (tag.equals("url"))
url = new String(p0, p1, p2);
else if (tag.equals("author"))
author = new String(p0, p1, p2);
else if (tag.equals("day"))
day = new String(p0, p1, p2);
else if (tag.equals("month"))
month = new String(p0, p1, p2);
else if (tag.equals("year"))
year = new String(p0, p1, p2);
else if (tag.equals("description"))
description = new String(p0, p1, p2);
}

private void parser() {
System.out.print("Content: ");
System.out.println(text);
System.out.print("URL: ");
System.out.println(url);
System.out.print("Author: ");
System.out.println(author);
System.out.print("Date: ");
System.out.println(day + "-" + month + "-" + year);
System.out.print("Description: ");
System.out.println(description);
System.out.println();
}

static public void main(String[] args) {
String filename = "url.xml";
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = null;
try {
saxParser = spf.newSAXParser();
saxParser.parse(new File(filename), new SAXXMLReader());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
摘自 Java教程