1
我想用Java中的sax解析器解析xml文件,然後用XML元素填充JTable
。解析完成,JTable
被創建,但問題是所有的元素被放置在同一行,並重復同一行。請任何幫助。這裏是我的代碼:將XML元素放入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>
而且表,我得到的是如下:[在這裏輸入的形象描述] [1]
我不明白什麼是錯誤!
1)對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程! 2)'![在這裏輸入圖片描述] [1]'那兒出了點問題。如果您有相關的屏幕截圖,請將其上傳到圖像共享網站並鏈接到它。 – 2013-04-11 06:34:02
我找不到您的圖片鏈接。 – trashgod 2013-04-11 11:19:49