-1

我必须解析一个 xml 文件并在 GUI 中显示所需的字段,我使用了 sax 解析器并在控制台中获得了所需的输出,但是除了 vname 之外,所有值都没有显示在 GUI 中,我将代码保留在下面。

在此处输入代码

import java.awt.Dimension;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.TextField;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
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;
import javax.swing.JTextArea;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JTable;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;

public class exp1 {
    public static String vname = "";
    public static String vvalue;
    public static String vtype = null;
    private final JLabel lblName = new JLabel("Name");
    private final JLabel lblType = new JLabel("Type");
    private final JLabel lblValue = new JLabel("Value");
    private final JLabel lblNewLabel = new JLabel("Results");
    private final TextArea textArea_3 = new TextArea();
    private final TextArea textArea = new TextArea();
    private final TextArea textArea_1 = new TextArea();

public  exp1 (String a){

    JFrame frame = new JFrame();
    frame.getContentPane().setFont(new Font("Times New Roman", Font.BOLD, 13));

    frame.setSize(new Dimension(668, 517));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    this.lblName.setFont(new Font("Times New Roman", Font.BOLD, 14));
    this.lblName.setBounds(86, 60, 56, 17);

    frame.getContentPane().add(this.lblName);

    this.lblType.setFont(new Font("Times New Roman", Font.BOLD, 14));
    this.lblType.setBounds(519, 60, 56, 17);

    frame.getContentPane().add(this.lblType);
    this.lblValue.setFont(new Font("Times New Roman", Font.BOLD, 14));
    this.lblValue.setBounds(297, 61, 46, 14);

    frame.getContentPane().add(this.lblValue);
    this.lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 16));
    this.lblNewLabel.setBounds(34, 11, 86, 38);

    frame.getContentPane().add(this.lblNewLabel);
    this.textArea_3.setBounds(27, 110, 212, 230);

    frame.getContentPane().add(this.textArea_3);
    this.textArea.setBounds(438, 110, 193, 230);

    frame.getContentPane().add(this.textArea);

    this.textArea_1.setBounds(256, 110, 168, 230);

    frame.getContentPane().add(this.textArea_1);
    frame.setVisible(true);

}

public static void main(String argv[]) {



exp1 xm = new exp1(null);



try {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {

boolean bfname = false;
boolean blname = false;
boolean bnname = 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;
        }

    if (qName.equalsIgnoreCase("VARIABLENAME")) {
        bnname = true;
        }
}

public void endElement(String uri, String localName,
String qName) throws SAXException {
}

public void characters(char ch[], int start, int length)
throws SAXException {

    if (bfname) {
        System.out.println("Type : "+ new String(ch, start, length));
        bfname = false;
        vtype = new String(ch, start, length);
        // VALUE HERE IS ONLY DISPLAYED ONCE IN JFRAME
        }

    if (nameAttribute != null && !nameAttribute.equals("")) {
        System.out.println("Name : " + nameAttribute);
        vname+=nameAttribute+ ", " +"\n";
        }

    if (blname) {

        vvalue = new String(ch, start, length);
        System.out.println("Value:" + Double.valueOf(vvalue));
        // VALUE HERE IS ONLY DISPLAYED ONCE IN JFRAME
        blname = false;
        }
}
};
saxParser.parse(new File("filepath.xml"), handler);  //for ex :-"New Folder\\VG_MachineData.xml"
xm.textArea_3.setText(vname);
xm.textArea.setText(vtype);
xm.textArea_1.setText(new Double(vvalue).toString());


} catch (Exception e) {
e.printStackTrace();
}
}


//xml tags
<?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>
4

2 回答 2

1

主要问题是setText在你JTextArea的 s. 这将覆盖字段中已有的任何文本并将其替换为您传递的内容。

由于您的大多数变量分配不涉及串联,因此您只设置了变量的最后一个值。

相反,您可以在方法中直接使用更新字段append

public void characters(char ch[], int start, int length)
        throws SAXException {

    System.out.println("characters " + new String(ch, start, length));
    if (bfname) {
        System.out.println("Type : " + new String(ch, start, length));
        bfname = false;
        vtype = new String(ch, start, length);
        xm.textArea_3.append(vtype + "\n");
    }

    if (nameAttribute != null && !nameAttribute.equals("")) {
        System.out.println("Name : " + nameAttribute);
        xm.textArea.append(nameAttribute + "\n");
    }

    if (blname) {

        vvalue = new String(ch, start, length);
        System.out.println("Value:" + Double.valueOf(vvalue));
        // VALUE HERE IS ONLY DISPLAYED ONCE IN JFRAME
        xm.textArea_1.append(new Double(vvalue).toString() + "\n");
        blname = false;
    }
}
于 2013-05-08T07:12:41.753 回答
0

我实现了一个示例代码。试试这种解决方案

package sax;

导入 javax.xml.parsers.SAXParser;导入 javax.xml.parsers.SAXParserFactory;

导入 org.xml.sax.Attributes;导入 org.xml.sax.SAXException;导入 org.xml.sax.helpers.DefaultHandler;

公共类 SaxTest {

public static void main(String args[]) {

    try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        DefaultHandler handler = new DefaultHandler() {
            String elementName = null;

            public void startElement(String uri, String localName,
                    String qName, Attributes attributes)
                    throws SAXException {
                /*
                 * this method is called once the parser meets a start
                 * element and you should implement here how your code
                 * should behave when a start element is found.The name of
                 * the start element is the parameter qName and the
                 * attributes of that element is under attributes parameter.
                 */
                elementName = qName;
            }

            public void endElement(String uri, String localName,
                    String qName) throws SAXException {
                /*
                 * this method is called once the parser meets an end
                 * element and you should implement here how your code
                 * should behave when an end element is found.The name of
                 * the end element is the parameter qName.
                 */
            }

            public void characters(char ch[], int start, int length)
                    throws SAXException {
                /*
                 * this method is called once the parser encounters text
                 * under an element.
                 */
                if (elementName.equals("vname")) {
                 //set the vname textarea here
                }
                if (elementName.equals("vtype")) {
                    //set the vtype textarea here
                }
                if (elementName.equals("vvalue")) {
                    //set the vvalue textarea here
                }
            }
        };

        // for specifying the xml document
        saxParser.parse("/home/sanjaya/Desktop/a.xml", handler);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
于 2013-05-08T06:59:39.243 回答