复制番号代番号次のように:
パッケージ com.test;
java.io.ファイルをインポートします。
java.util.ArrayListをインポートします。
java.util.Iteratorをインポートします。
java.util.Listをインポートします。
org.dom4j.Document をインポートします。
org.dom4j.Element をインポートします。
org.dom4j.io.SAXReader をインポートします。
パブリック クラス Dom4jXML {
public static void main(String[] args) {
SAXReader saxReader = new SAXReader();
ファイル file = new File("e:/People.xml");
試す {
ドキュメント document = saxReader.read(file);
要素ルート = document.getRootElement();
List<People> peoples = new ArrayList<People>();
for (イテレータ iterator = root.elementIterator(); iterator.hasNext();) {
人々 人々 = new People();
要素 peopleElement = (要素) iterator.next();
people.setId(peopleElement.attribute("id").getValue());
for (Iterator childIterator = peopleElement.elementIterator(); childIterator.hasNext();) {
要素 childPeopleElement = (要素) childIterator.next();
if (childPeopleElement.getName().equals("名前")) {
people.setEnglishName(childPeopleElement.attribute("en").getValue());
people.setName(childPeopleElement.getText());
else if (childPeopleElement.getName().equals("Age")) {
people.setAge(childPeopleElement.getText());
}
}
peoples.add(人);
}
for (人 p : 人) {
System.out.println(p.getId()+"/t"+p.getName()+"/t"+p.getEnglishName()+"/t"+p.getAge());
}
} catch (例外 e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}