libreria - ¿Cómo mostrar el resultado en JFrame Form si el origen de datos es un archivo XML?
libreria dom java (0)
Aquí están los códigos:
Escribo un archivo XML en un comentario debajo de esta publicación.
Desarrollador:
public class Developer {
private String id;
private String name;
private String surname;
private int age;
public Developer(String id, String name, String surname, int age) {
this.id = id;
this.name = name;
this.surname = surname;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public int getAge() {
return age;
}
}
El método:
public static void main(String[] args) throws Exception {
File xmlFile = new File("data.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document = documentBuilder.parse(xmlFile);
NodeList list = document.getElementsByTagName("Developer");
List<Developer> developers = new ArrayList<>();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
Developer developer = new Developer(
element.getAttribute("Id"),
element.getElementsByTagName("Name").item(0).getTextContent(),
element.getElementsByTagName("Surname").item(0).getTextContent(),
Integer.parseInt(element.getElementsByTagName("Age").item(0).getTextContent())
);
developers.add(developer);
}
}
// at this point we have a list of developers
// print out all the developers
for (Developer developer : developers) {
System.out.println("ID: " + developer.getId());
System.out.println("Name: " + developer.getName());
System.out.println("Surname: " + developer.getSurname());
System.out.println("Age: " + developer.getAge());
}
}
Código JFrame:
public class window extends javax.swing.JFrame {
public window() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
rbClick = new javax.swing.JButton();
lbResult = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
rbClick.setText("Click");
lbResult.setText("Result");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(144, 144, 144)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lbResult)
.addComponent(rbClick))
.addContainerGap(203, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(rbClick)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbResult)
.addContainerGap(202, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new window().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel lbResult;
private javax.swing.JButton rbClick;
// End of variables declaration
}
¿Cómo hacer lo siguiente? Si hago clic en el botón rbClick, en lbResult solo se muestran las personas de 18 a 24 años de edad.