I have been reading allot about it and I have tried different ways but I'm unsure of how to actually read data from xml files using XPath.
I want to store the names of my buttons in xml files, instead of using the current arrays private String[] buttonNameVar = {"Integer", "Float", "Boolean", "Char", "String", "Array", "Byte"}; private String[] buttonNameCon = {"If", "Else", "If Else", "Switch"}; private String[] buttonNameLoop = {"For", "While", "Do ...While"};
Is it possible to get data from the xml files and store it in the arrays?
here is my code:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class ExampleOfButtons extends JPanel{
private JPanel buttonPanelVar, buttonPanelCon, buttonPanelLoop;
private String[] buttonNameVar = {"Integer", "Float", "Boolean", "Char", "String", "Array", "Byte"};
private String[] buttonNameCon = {"If", "Else", "If Else", "Switch"};
private String[] buttonNameLoop = {"For", "While", "Do ...While"};
private String file = "Resources/SettingsFiles/buttonNames.xml";
private File xmlFile;
private Document xmlDoc;
private String result = null;
public ExampleOfButtons() {
setLayout(new GridLayout(3,1));
buttonPanelVar = new JPanel();
buttonPanelCon = new JPanel();
buttonPanelLoop = new JPanel();
buttonPanelVar.setLayout(new GridLayout(7,1));
buttonPanelCon.setLayout(new GridLayout(4,1));
buttonPanelLoop.setLayout(new GridLayout(3,1));
buttonPanelVar.setBorder((Border) new TitledBorder(null, "Variables"));
buttonPanelCon.setBorder((Border) new TitledBorder(null, "Conditions"));
buttonPanelLoop.setBorder((Border) new TitledBorder(null, "Loop"));
Color color = new Color(174, 243, 252);
buttonPanelVar.setBackground(color);
buttonPanelCon.setBackground(color);
buttonPanelLoop.setBackground(color);
xmlFile = new File(file);
try {
xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
} catch (SAXException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
xmlDoc.getDocumentElement().normalize();
XPath x = XPathFactory.newInstance().newXPath();
XPathExpression ex = x.compile(\buttons\variables);
addButtOne(buttonPanelVar);
addButtTwo(buttonPanelCon);
addButtThree(buttonPanelLoop);
add(buttonPanelVar);
add(buttonPanelCon);
add(buttonPanelLoop);
}
private void addButtThree(JPanel buttonPanelLoop) {
for (int i = 0; i < buttonNameLoop.length; i++) {
JButton button = new JButton(buttonNameLoop[i]);
buttonPanelLoop.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
buttonListener(e);
}
});
addToolTip(button);
}
}
private void addButtTwo(JPanel buttonPanelTwo) {
for (int i = 0; i < buttonNameCon.length; i++) {
JButton button = new JButton(buttonNameCon[i]);
buttonPanelTwo.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
buttonListener(e);
}
});
addToolTip(button);
}
}
private void addButtOne(JPanel buttonPanelVar) {
for (int i = 0; i < buttonNameVar.length; i++) {
JButton button = new JButton(buttonNameVar[i]);
buttonPanelVar.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
buttonListener(e);
}
});
addToolTip(button);
}
}
public void buttonListener(ActionEvent e) {
String name = e.getActionCommand();
if(name == "Integer"){
System.out.println("Integer");
}
if(name == "Float"){
System.out.println("Float");
}
if(name == "Boolean"){
System.out.println("Boolean");
}
if(name == "Char"){
System.out.println("Char");
}
if(name == "String"){
System.out.println("String");
}
if(name == "Array"){
System.out.println("Array");
}
if(name == "Byte"){
System.out.println("Byte");
}
if(name == "If"){
System.out.println("If");
}
if(name == "Else"){
System.out.println("Else");
}
if(name == "If Else"){
System.out.println("If Else");
}
if(name == "Switch"){
System.out.println("Switch");
}
if(name == "For"){
System.out.println("For");
}
if(name == "While"){
System.out.println("While");
}
if(name == "Do ...While"){
System.out.println("Do ...While");
}
}
public void addToolTip(JButton button){
button.setToolTipText("Tool Tip");
}
}
After the XPathExpressions
I can use something like Node n =(Node)ex.evaluate(xmlDoc, XPathConstants.NODE); results = n.getTextContent();
also here is the xml file I have made
<?xml version="1.0" encoding="UTF-8"?>
<Buttons>
<variables>
<var_one>Integer</var_one>
<var_two>Float</var_two>
<var_three>Boolean</var_three>
<var_four>Char</var_four>
<var_five>String</var_five>
<var_six>Array</var_six>
<var_seven>Byte</var_seven>
</variables>
<conditions>
<con_one>If</con_one>
<con_two>Else</con_two>
<con_three>If Else</con_three>
<con_four>Switch</con_four>
</conditions>
<loops>
<loop_one>For</loop_one>
<loop_two>While</loop_two>
<loop_three>Do ...While</loop_three>
</loops>
</Buttons>
No comments:
Post a Comment