import java.awt.GridLayout;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@SuppressWarnings({ "deprecation", "unused" })
public class SAXHandler extends HandlerBase {
private JFrame frame = new JFrame("学员信息");
private JLabel[] labels = new JLabel[4];
private JTextField[] fields = new JTextField[4];
private Hashtable<String, String> table = new Hashtable<String, String>();
private String currentElement = "";
private String currentValue = "";
public Hashtable<String, String> 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 String getCurrentElement() {
return currentElement;
}
public void setCurrentElement(String currentElement) {
this.currentElement = currentElement;
}
public String getCurrentValue() {
return currentValue;
}
public void setCurrentValue(String currentValue) {
this.currentValue = currentValue;
}
public void setTable(Hashtable<String, String> table) {
this.table = table;
}
public void endElement(String name) throws SAXException {
if (currentElement.equals(name)) {
table.put(currentElement, currentValue);
}
}
public String currentValue(String... strings) throws SAXException,
IOException, ParserConfigurationException {
SAXParserFactory.newInstance().newSAXParser().parse(
new InputSource(new FileReader(new File(strings[0]))), this);
return this.getTable().get(strings[1]);
}
@SuppressWarnings("static-access")
public SAXHandler() throws SAXException, IOException,
ParserConfigurationException {
labels[0] = new JLabel("name");
labels[1] = new JLabel("college");
labels[2] = new JLabel("telphone");
labels[3] = new JLabel("notes");
String[] strings = {"people.xml",
labels[0].getText() };
fields[0] = new JTextField(currentValue(strings[0], strings[1]));
strings[1] = labels[1].getText();
fields[1] = new JTextField(currentValue(strings[0], strings[1]));
strings[1] = labels[2].getText();
fields[2] = new JTextField(currentValue(strings[0], strings[1]));
strings[1] = labels[3].getText();
fields[3] = new JTextField(currentValue(strings[0], strings[1]));
for (int i = 0; i < 4; i++) {
frame.add(labels[i]);
frame.add(fields[i]);
}
frame.setLayout(new GridLayout(4, 2));
frame.pack();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException {
new SAXHandler();
}
}
<?xml version="1.0" encoding="GB18030"?> <person> <name>王小明</name> <college>信息学院</college> <telphone>6258113</telphone> <notes>男,1955年生,博士,95年调入海南大学</notes> </person>