2013-05-08 73 views
-1

我要解析的XML文件,并显示在图形用户界面所需的字段我已经使用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> 
+0

您可以提供[SSCCE](http://sscce.org/),因为您的代码没有意义。我可以在你的问题猜测,但只是将每个人的时间浪费... – MadProgrammer 2013-05-08 06:42:33

+0

我已编辑全码的问题,以下是XML – nick 2013-05-08 07:00:32

回答

1

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

由于大部分的变量赋值并没有涉及拼接,你只设置变量的最后一个值。

相反,您可以在该方法中直接使用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; 
    } 
} 
+0

程序员感谢两次。我可以知道我的代码中的问题是什么,当我根据你改变时它工作得很好。 – nick 2013-05-08 07:58:20

+0

的主要问题是你分配你的变量的,你在做像'VVALUE =新的String的东西(CH,开始位置,长度);',这意味着'vvalue'总是只会等于分配给最后一个值它。我认为'vname'是唯一一个你连接的... – MadProgrammer 2013-05-08 08:01:54

+0

Programmer Thanks Bro。 – nick 2013-05-08 08:45:36

0

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

package sax; 

进口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;

公共类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(); 
    } 
} 
} 
+0

问题是,当所有的数值分析所显示的字符串只在VNAME vtype和vvalue没有在GUI中设置。我已经用xml保存了我的完整代码 – nick 2013-05-08 07:09:32