我必须将来自 sax 解析器的数据存储在数组中,但它显示 nullreal 值,最后它显示 Array out of bound 异常。如何在数组中添加所有数据类型,如名称属性 vtype 和值。
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import org.xml.sax.Attributes;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class JTabbedPaneDemo extends JPanel {
/*static String[] nameatt= {" sv_machineData "," Mould_data "," Core_1 "," Core_2 "};*/
public JTabbedPaneDemo() {
// ImageIcon icon = new ImageIcon("java-swing-tutorial.JPG");
JTabbedPane jtbExample = new JTabbedPane();
// Create First Tab.
JPanel jplInnerPanel1 = createInnerPanel();
jtbExample.addTab(" Sv_Machine_Data ", jplInnerPanel1);
jplInnerPanel1.setLayout(new BorderLayout());
JTable table1 = new JTable(5,3);
JScrollPane tableContainer = new JScrollPane(table1);
jplInnerPanel1.add(tableContainer,BorderLayout.CENTER);
//jtbExample.setSelectedIndex(0);
// Create Second Tab.
JPanel jplInnerPanel2 = createInnerPanel();
jtbExample.addTab(" Mould_data ", jplInnerPanel2);
jplInnerPanel2.setLayout(new BorderLayout());
JTable table2 = new JTable(5,3);
JScrollPane tableContainer1 = new JScrollPane(table2);
jplInnerPanel2.add(tableContainer1,BorderLayout.CENTER);
// Create Third Tab.
JPanel jplInnerPanel3 = createInnerPanel();
jtbExample.addTab(" Core_1 ", jplInnerPanel3);
jplInnerPanel3.setLayout(new BorderLayout());
JTable table3 = new JTable(5,3);
JScrollPane tableContainer3 = new JScrollPane(table3);
jplInnerPanel3.add(tableContainer3,BorderLayout.CENTER);
// Create Fourth Tab.
JPanel jplInnerPanel4 = createInnerPanel();
jtbExample.addTab(" Core_2 ", jplInnerPanel4);
jplInnerPanel4.setLayout(new BorderLayout());
JTable table4 = new JTable(5,3);
JScrollPane tableContainer4 = new JScrollPane(table4);
jplInnerPanel4.add(tableContainer4,BorderLayout.CENTER);
// Add the Tabbed Pane to this panel.
setLayout(new GridLayout(1, 1));
add(jtbExample);
}
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;
}
public static void main(String[] args) {
final String[] vt= {};
JFrame frame = new JFrame("TabbedPane Source Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
/*final JTabbedPaneDemo xm = new JTabbedPaneDemo();*/
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean bfname = false;
boolean blname = false;
String nameAttribute;
public void startElement(String uri, String localName,String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("TYP")) {
bfname = true;
}
nameAttribute = attributes.getValue("Name");
if (qName.equalsIgnoreCase("VALUE")){
blname = true;
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
if (bfname) {
System.out.println("Type : "+ new String(ch, start, length));
String vtype = new String(ch, start, length);
String [] typ = new String[length] ;
for(int i=0;i<=length;i++) {
typ[i]+=vtype;
System.out.println("Type is "+typ[i]);
}
/*System.out.println("value of vt" +vt[j]);*/
/*xm.textArea.append(vtype+"\n");*/
bfname = false;
}
if (nameAttribute != null && !nameAttribute.equals("")) {
//System.out.println("Name : " + nameAttribute);
/*vname+=nameAttribute+ ", " +"\n";
xm.textArea_3.setText(vname);
*/}
if (blname) {
String vvalue = new String(ch, start, length);
//System.out.println("Value:" + Double.valueOf(vvalue));
/*xm.textArea_1.append(Double.valueOf(vvalue)+"\n");*/
blname = false;
}
}
};
saxParser.parse(new File("my xmllocal address.xml"), handler);
}
catch (Exception e) {
e.printStackTrace();
}
frame.getContentPane().add(new JTabbedPaneDemo(),BorderLayout.CENTER);
frame.setSize(400, 500);
frame.setVisible(true);
}
示例 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>