我試圖獲取示例XML文件中每個人的姓名,但我獲取的是null值而不是他們的姓名。爪哇代碼:package testjdom;import java.io.IOException;import java.util.List;import org.jdom2.Document;import org.jdom2.Element;import org.jdom2.JDOMException;import org.jdom2.input.SAXBuilder;public class TestJDOM { public static void main(String[] args) throws JDOMException, IOException { SAXBuilder jdomBuilder = new SAXBuilder(); Document jdomDocument = jdomBuilder.build("persons.xml"); Element jdomRoot = jdomDocument.getRootElement(); List<Element> children = jdomRoot.getChildren(); for (Element child : children) { System.out.println(child.getAttributeValue("name")); } }}XML:<?xml version="1.0" encoding="UTF-8"?><persons> <person> <id>1</id> <name>The Best</name> <email>thenextbigthing@gmail.com</email> <birthDate>1981-11-23</birthDate> </person> <person> <id>2</id> <name>Andy Jr.</name> <email>usa@gmail.com</email> <birthDate>1982-12-01</birthDate> </person> <person> <id>3</id> <name>JohnDoe</name> <email>gameover@gmail.com</email> <birthDate>1990-01-02</birthDate> </person> <person> <id>4</id> <name>SomeOne</name> <email>rucksack@gmail.com</email> <birthDate>1988-01-22</birthDate> </person> <person> <id>5</id> <name>Mr. Mxyzptlk</name> <email>bigman@hotmail.com</email> <birthDate>1977-08-12</birthDate> </person></persons>我怎樣才能得到每個名字的真正價值?最終我想從 XML 中獲取每個人的四個值。我有一個名為 Person 的類,它具有與 XML 文件中的人員相同的屬性、id、名稱等。我想從“Person”類創(chuàng)建新對象,并使用XML 中的數(shù)據(jù)。當(dāng)我創(chuàng)建一個新對象并使用我從 XML 獲得的值成功設(shè)置其屬性時,我想將該對象添加到 ArrayList 然后對其余人員重復(fù)相同的過程。
添加回答
舉報
0/150
提交
取消