public class SAXHandler extends HandlerBase {
private Hashtable table = new Hashtable();
private String currentElement = null;
private String currentValue = null;
private List list = new ArrayList();
private int nodes = 0; public void setTable(Hashtable table) {
this.table = table;
} public Hashtable getTable() {
return table;
} public void startElement(String tag, AttributeList attrs)
throws SAXException {
currentElement = tag;
} public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue = new String(ch, start, length);
} public void endElement(String name) throws SAXException {
if (currentElement.equals(name)) {
table.put(currentElement, currentValue); }
if (++nodes == 7) {
list.add(table);
table = new Hashtable();
nodes = 0;
}
}
public List getList() {
return list;
}
} java.io.File file = new java.io.File("aaa.xml");
FileReader reader = new FileReader(file);
Parser parser;
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
SAXHandler handler = new SAXHandler();
sp.parse(new InputSource(reader), handler);
List list = handler.getList();
for (int i = 0; i < list.size(); i++) {
Map cfgTable =(Map)(list.get(i));
//注释的代码也可以用
// title = (String) cfgTable.get(new String("名称"));
// descrip = (String) cfgTable.get(new String("描述"));
// type = (String) cfgTable.get(new String("类别"));
// url = (String) cfgTable.get(new String("网址"));
// images = (String) cfgTable.get(new String("配图"));
// action = (String) cfgTable.get(new String("指令"));
Set set = cfgTable.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s + "=" + cfgTable.get(s)); }
System.out.println("****************");
}
private Hashtable table = new Hashtable();
private String currentElement = null;
private String currentValue = null;
private List list = new ArrayList();
private int nodes = 0; public void setTable(Hashtable table) {
this.table = table;
} public Hashtable getTable() {
return table;
} public void startElement(String tag, AttributeList attrs)
throws SAXException {
currentElement = tag;
} public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue = new String(ch, start, length);
} public void endElement(String name) throws SAXException {
if (currentElement.equals(name)) {
table.put(currentElement, currentValue); }
if (++nodes == 7) {
list.add(table);
table = new Hashtable();
nodes = 0;
}
}
public List getList() {
return list;
}
} java.io.File file = new java.io.File("aaa.xml");
FileReader reader = new FileReader(file);
Parser parser;
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
SAXHandler handler = new SAXHandler();
sp.parse(new InputSource(reader), handler);
List list = handler.getList();
for (int i = 0; i < list.size(); i++) {
Map cfgTable =(Map)(list.get(i));
//注释的代码也可以用
// title = (String) cfgTable.get(new String("名称"));
// descrip = (String) cfgTable.get(new String("描述"));
// type = (String) cfgTable.get(new String("类别"));
// url = (String) cfgTable.get(new String("网址"));
// images = (String) cfgTable.get(new String("配图"));
// action = (String) cfgTable.get(new String("指令"));
Set set = cfgTable.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s + "=" + cfgTable.get(s)); }
System.out.println("****************");
}
OK了。十分感谢asjj(叫我ALT+F4)的多次帮助。msn是多少?加一个吧,我的是[email protected] (钟林-原为IPTV事业作贡献)
我要下班了,明天上午在上msn