gpt4 book ai didi

java - 将 XML 元素放入 JTable 中

转载 作者:行者123 更新时间:2023-11-30 04:25:34 25 4
gpt4 key购买 nike

我正在尝试使用 Java 中的 sax 解析器解析 XML 文件,然后用 XML 元素填充 JTable 。解析完成,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>

最佳答案

在您的 startElement 方法中,更改为:

if (qName.equalsIgnoreCase("staff"))
{
rowOne = new Vector<String>();
bstaff = true;
}

其他 if 语句保持不变。每个 staff 元素都放入一个 Vector 中。

关于java - 将 XML 元素放入 JTable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15942056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com