我正在尝试使用 Java 中的 sax 解析器解析 XML 文件,然后JTable
用 XML 元素填充 a。解析好了,JTable
创建了,但问题是所有元素都放在同一行,重复同一行。
这是我的代码:
package read;
//import java.util.ArrayList;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Read {
public static Vector<Vector> rowData = new Vector<Vector>();
public static Vector<String> rowOne = new Vector<String>();
public static void main(String argv[]) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler;
handler = new DefaultHandler() {
// private List<Personne> satff;
boolean bstaff = false;
boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;
private int i;
public void startElement(String uri, String localName, String
qName, Attributes attributes) throws SAXException {
System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("staff")) {
bstaff = true;
}
if (qName.equalsIgnoreCase("FIRSTNAME")) {
bfname = true;
}
if (qName.equalsIgnoreCase("LASTNAME")) {
blname = true;
}
if (qName.equalsIgnoreCase("NICKNAME")) {
bnname = true;
}
if (qName.equalsIgnoreCase("SALARY")) {
bsalary = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
System.out.println("End Element :" + qName);
if ("staff".equals(qName)) {
rowData.addElement(rowOne);
};
}
@Override
public void characters(char ch[], int start, int length) {
if (bfname) {
String s = new String(ch, start, length);
rowOne.addElement(s);
System.out.println("First Name : " + new String(ch, start, length));
bfname = false;
}
if (blname) {
rowOne.addElement(new String(ch, start, length));
System.out.println("Last Name : " + new String(ch, start, length));
blname = false;
}
if (bnname) {
rowOne.addElement(new String(ch, start, length));
System.out.println("Nick Name : " + new String(ch, start, length));
bnname = false;
}
if (bsalary) {
rowOne.addElement(new String(ch, start, length));
System.out.println("Salary : " + new String(ch, start, length));
//rowData.addElement(rowOne);
bsalary = false;
}
System.out.println("longueur" + rowOne.size());
}
};
saxParser.parse("file.xml", handler);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("firstname");
columnNames.addElement("lastname");
columnNames.addElement("nickname");
columnNames.addElement("salary");
columnNames.addElement(" ");
columnNames.addElement(" ");
columnNames.addElement(" ");
columnNames.addElement(" ");
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的原始 XML 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<company>
<staff >
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff >
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>