默认处理程序存储错误

时间:2013-05-16 05:32:35

标签: java xml swing parsing

我必须将来自sax解析器的数据存储在数组中,但是它显示了nullreal值,最后它显示了Array out of bound的异常。如何在数组中添加所有数据类型,如name属性vtype和value。

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>

1 个答案:

答案 0 :(得分:2)

这是问题所在:

 for(int i=0;i<=length;i++) {
     typ[i]+=vtype;
     System.out.println("Type is "+typ[i]);
 }

您在每个值的开头都得到null因为typ[i]在连接之前为空。

您收到异常是因为您使用的是<=而不是<,所以在最后一次迭代中,i将等于length,{{1}因此将无效。

可以用以下方法修复循环:

typ[i]

......但似乎毫无意义。我认为没有理由首先创建一个字符串数组。

如果您需要数组中的所有类型,则需要创建该数组(或理想情况下的列表)外部 for (int i = 0;i < length; i++) { typ[i] = vtype; System.out.println("Type is " + typ[i]); } 方法... characters方法为每段文字调用。您需要将列表创建为正在进行的状态,并在characters方法中添加

除非这是一个非常庞大的文件,否则我会亲自从SAX开始 - 虽然它对于一次性读取大文件非常强大,但与将整个文件加载到某种DOM相比,这是一件很麻烦的事情。表示(可能是第三方XML库,如JDOM)。