我正在制作一个 xml 解析器,其中从文件中读取 xml 问题是我必须在运行时动态设置选项卡,其中标题是从 xml 文件中获取的,而且我必须在显示它们之前拆分解析字符串的某些部分JTable。我将我的代码保留在下面:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class XmlGui extends JFrame {
public XmlGui() {/*Default Constructor*/}
public static void main(String[] args) throws SAXException, IOException,ParserConfigurationException {
int check = 0;
String TabTitle;
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XmlBack xmlBack = new XmlBack();
saxParser.parse(new File("C:\\Documents and Settings\\ASU\\Desktop\\Xml File\\VG_MachineData.xml"), xmlBack);
new XmlGui().drawFrame(xmlBack.varGroupVariables);
}
public void drawFrame(List<VarGroupVariable> varGroupVariables) {
setTitle("The Xml Parser");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(800, 500);
JTabbedPane TabbedPane = new JTabbedPane();
JPanel jplInnerPanel1 = createInnerPanel();
jplInnerPanel1.setLayout(new BorderLayout());
//Vector<String> columnNames = new Vector<String>();
JTable table = new JTable();
table.setModel(new VarGroupVariableModel(varGroupVariables));
table.setFont(new Font("Times New Roman", Font.BOLD, 16));
// Create Second Tab.
JPanel jplInnerPanel2 = createInnerPanel();
TabbedPanePane.addTab(" DEMO TABBED ", jplInnerPanel2);
jplInnerPanel2.setLayout(new BorderLayout());
}
protected JPanel createInnerPanel() {
JPanel jplPanel = new JPanel();
JLabel jlbDisplay = new JLabel();
jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
jplPanel.setLayout(new GridLayout(1, 1));
jplPanel.add(jlbDisplay);
return jplPanel;
}
}
sample xml
<?xml version="1.0" encoding="UTF-8"?>
<HMI_Data Version="1.0" MaschinenNR.="XXXXXX" Date="21-10-2009">
<VarGroup Name="VG_MachineData">
<Variable Name="Mold1.sv_rMoldStroke">
<Typ>REAL</Typ>
<Value>6.000000e+02</Value>
</Variable>
<Variable Name="Core1.sv_rMaxSpeedFwd">
<Typ>REAL</Typ>
<Value>5.000000e+01</Value>
</Variable>
<Variable Name="Core1.sv_rMaxSpeedBwd">
<Typ>REAL</Typ>
<Value>5.000000e+01</Value>
</Variable>
<Variable Name="Core1.sv_rMaxPressureFwd">
<Typ>REAL</Typ>
<Value>1.450000e+02</Value>
</Variable>
<Variable Name="Core1.sv_rMaxPressureBwd">
<Typ>REAL</Typ>
<Value>1.450000e+02</Value>
</Variable>
<Variable Name="Core1.sv_rImpulseFilterLimit">
<Typ>REAL</Typ>
<Value>0.000000e+00</Value>
</Variable>
提前致谢。