1

我必须将来自 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>
4

1 回答 1

2

这就是问题:

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

您正在null每个值的开头,因为typ[i]在连接之前为 null 。

你得到一个例外,因为你在最后一次迭代中使用,<=而不是so,将 equal ,因此将是无效的。<ilengthtyp[i]

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

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

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

如果您需要数组中的所有类型,则需要在方法之外创建该数组(或理想情况下是列表)......characters每段characters文本调用该方法。您需要将列表创建为持续状态,然后在方法中添加到它characters

除非这真的是一个巨大的文件,否则我个人会从 SAX 开始 - 虽然它对于一次性读取的大文件非常强大,但与将整个文件加载到某种 DOM 表示形式(可能第三方 XML 库,例如 JDOM)。

于 2013-05-16T05:42:06.963 回答