目前,这就是我的XML文件的样子。还有10个学生,但我认为你不需要看到它。
<Student id="11111">
<FirstName>Joe</FirstName>
<LastName>Edwards</LastName>
<Address>
<Street>345 North</Street>
<City>Brea</City>
<State>CA</State>
<Zip>99999</Zip>
<Phone>(714) 444-4444</Phone>
</Address>
<Classes>
<Class crn="5555">
<Name>CIS 100</Name>
<Units>3</Units>
<Grade>A</Grade>
</Class>
<Class crn="7777">
<Name>CIS 111</Name>
<Units>4</Units>
<Grade>B</Grade>
</Class>
</Classes>
</Student>
这是我的代码,分别“提取”名字和姓氏。
NodeList studentlist = doc.getElementsByTagName("Student");
for(int i = 0; i < studentlist.getLength(); i++)
{
Node n = studentlist.item(i);
Element element = (Element)n;
String id = element.getAttribute("id");
Student obj = new Student(id);
NodeList slist = n.getChildNodes();
for (int j = 0; j < slist.getLength(); j++)
{
Node selement = slist.item(j);
if (selement.getNodeType() == Node.ELEMENT_NODE)
{
String textval = selement.getTextContent();
if (selement.getNodeName().equals("FirstName"))
{
obj.setFirstname(textval);
}
if (selement.getNodeName().equals("LastName"))
{
obj.setLastname(textval);
}
}
}
students.add(obj);
}
我的学生班:
public class Student
{
private String id;
private String firstname;
private String lastname;
private Address adr;
private ArrayList<Course> courses;
我的问题是:如何为同一个对象添加“地址”和“类”节点?
答案 0 :(得分:2)
因此,解决这个问题的一种方法是为Address和Class创建数据结构,如下所示:
public class Address
{
private String street;
private String city;
private String state;
private String zip;
private String phone;
}
和(注意:调用Java类“Class”可能会非常令人困惑,所以请使用其他我想说的内容)
public class Course
{
private int crn;
private String name;
private String units;
private String grade;
}
(你也必须为这两个类创建构造函数)
然后,在你的for循环中,在外部“if”块中,在检查节点名称“FirstName”和“LastName”的“if”语句之后,你可以这样做:
if (selement.getNodeName().equals("Address"))
{
String street = null;
String city = null;
String state = null;
String zip = null;
String phone = null;
NodeList addressChildren = selement.getChildNodes();
for(int k=0; k < addressChildren.getLength(); k++) {
Node addrNode = addressChildren.item(k);
if(addrNode.getNodeName().equals("Street")
{
street = addrNode.getTextContent();
}
...//etc for City, State, Zip, and Phone
Address a = new Address(street, city, state, zip, phone)
obj.setAddress(a);
}
}
if (selement.getNodeName().equals("Classes"))
{
NodeList classNodes = selement.getChildNodes();
Course[] courses = new Course[classNodes.getLength());
for(int l=0; l < classNodes.getLength(); l++) {
Node classNode = classNodes.item(l);
NodeList classChildren = classNode.getChildNodes();
for(int m=0; m < classChildren.getLenght(); m++)
{
Node classChild = classChildren.item(i);
int crn = Integer.parseInt(classChild.getAttributes().getNamedItem("crn").getTextContent());
String name = null;
String units = null;
if(classChild.getNodeName().equals("Name")
{
name = classChild.getTextContent();
}
if(classChild.getNodeName().equals("Units")
{
units = classChild.getTextContent();
}
courses[m] = new Course(crn, name, units);
}
obj.setCourses(Arrays.asList(courses));
}
}
好的,所以上面的代码并不完美(我在浏览器中写过),但我认为,基于你已经想到的XML解析,这是一个好的开始。