学员信息(用sax解析)

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值