我要解析的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>
您可以提供[SSCCE](http://sscce.org/),因爲您的代碼沒有意義。我可以在你的問題猜測,但只是將每個人的時間浪費... – MadProgrammer 2013-05-08 06:42:33
我已編輯全碼的問題,以下是XML – nick 2013-05-08 07:00:32