我想将具有相同标签的对象保留在xml文件的一个列表中。例如,对于以下xml文件,每个员工都应具有一个List<Integer>
属性,该属性将存储该员工的所有<age>
值。
<?xml version="1.0"?>
<Employees>
<Employee>
<name>Pankaj</name>
<age>29</age>
<age>42</age>
<role>
Java Developer
Musician
Artist
</role>
<gender>Male</gender>
</Employee>
<Employee>
<name>Lisa</name>
<age>35</age>
<age>23</age>
<role>CSS Developer</role>
<gender>Female</gender>
</Employee>
</Employees>
这是我的Employee类代码:
import java.util.ArrayList;
import java.util.List;
public class Employee {
private String name;
private String gender;
private List<Integer> age = new ArrayList<Integer>();
private String role;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public List<Integer> getAge() {
return this.age;
}
public void addAge(int age) {
this.age.add(age);
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return "Employee:: Name=" + this.name +" Gender=" + this.gender +
" Role=" + this.role;
}
public void agetoString() {
for(Integer my_age : this.age) {
System.out.println(my_age + " ");
}
}
}
XMLReaderDOM
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLReaderDOM {
public static void main(String[] args) {
String filePath = "employee.xml";
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nodeList = doc.getElementsByTagName("Employee");
//now XML is loaded as Document in memory, lets convert it to Object List
List<Employee> empList = new ArrayList<Employee>();
for (int i = 0; i < nodeList.getLength(); i++) {
empList.add(getEmployee(nodeList.item(i)));
}
//lets print Employee list information
for (Employee emp : empList) {
System.out.println(emp.toString());
emp.agetoString();
}
} catch (SAXException | ParserConfigurationException | IOException e1) {
e1.printStackTrace();
}
}
private static Employee getEmployee(Node node) {
//XMLReaderDOM domReader = new XMLReaderDOM();
Employee emp = new Employee();
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
emp.setName(getTagValue("name", element));
emp.addAge(Integer.parseInt(getTagValue("age", element)));
emp.setGender(getTagValue("gender", element));
emp.setRole(getTagValue("role", element));
}
return emp;
}
private static String getTagValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodeList.item(0);
return node.getNodeValue();
}
}
程序仅返回的第一个匹配项,但我需要它返回完整列表。
Root element :Employees
Employee:: Name=Pankaj Gender=Male Role=
Java Developer
Musician
Artist
29
Employee:: Name=Lisa Gender=Female Role=CSS Developer
35
这是我第一次使用xml文件。您能帮我一下,告诉我我做错了什么。预先谢谢你。