英文:
Read multiple child of Element in XML using STAX JAVA
问题
这是你想要的输出的方法:
public static void displayPeopleWithItems() {
ArrayList<Person> people = personDAO.getPeople();
if (people != null) {
for (Person person : people) {
System.out.println(person.getName());
for (String item : person.getItems()) {
System.out.println(" " + item);
}
}
}
}
请确保 Person
类有一个名为 getItems()
的方法,以返回该人的所有项目作为字符串列表。
英文:
How do I read all childrem of Item element and store the in an object
This is my sample xml file
<People>
<Person>
<Name>Ben</Name>
<Items>
<Item>Pen</Item>
<Item>Paper</Item>
<Item>Books</Item>
</Items>
</Person>
<Person>
<Name>Alex</Name>
<Items>
<Item>Pencil</Item>
<Item>Eraser</Item>
</Items>
</People>
I made a Person object with getters and setters
public class Person{ // SAMPLE OBJECT
private String name; @getters and setters
private String item; @getters and setters
}
This is my method where it read
public ArrayList<Person> getPeople(){
people = new ArrayList<>();
Person a = null;
if (Files.exists(peoplePath)) // prevent the FileNotFoundException
{
// create the XMLInputFactory object
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
try
{
// create a XMLStreamReader object
FileReader fileReader =
new FileReader(peoplePath.toFile());
XMLStreamReader reader =
inputFactory.createXMLStreamReader(fileReader);
// read from the file
while (reader.hasNext())
{
int eventType = reader.getEventType();
switch (eventType)
{
case XMLStreamConstants.START_ELEMENT:
String elementName = reader.getLocalName();
if (elementName.equals("Person"))
{
a = new Person();
}
if (elementName.equals("Name"))
{
String name = reader.getElementText();
a.setName(name);
}
if (elementName.equals("Item"))
{
String item= reader.getElementText();
a.setItem(item);
}
break;
case XMLStreamConstants.END_ELEMENT:
elementName = reader.getLocalName();
if (elementName.equals("Person"))
{
people.add(a);
}
break;
default:
break;
}
reader.next();
}
}
catch (IOException | XMLStreamException e)
{
System.out.println(e);
return null;
}
}
return people;
}
This method displays <br>
Ben<br>
Alex
but if I get the items it only using similar code, it displays <br>
Books<br>
Eraser
public static void displayPeople(){
ArrayList<Person> people = personDAO.getPeople();
People p = null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < people.size(); i++)
{
a = people.get(i);
sb.append(a.getName());
sb.append("\n");
}
System.out.println(sb.toString());
}
I wanted to do this output
Ben
Pen
Paper
Book
Alex
Pencil
Eraser
答案1
得分: 0
首先,你的xml在结束之前缺少了Person元素的关闭标签。
</Person>
其次,你的Person类不太正确。Items应该是一个String列表,而不只是一个String。
public class Person {
// 为简洁起见,私有关键字已被删除
String name;
List<String> items = new ArrayList<>();
}
第三,你的代码需要更新以使用项目列表,如下所示:
if (elementName.equals("Item")) {
String item = reader.getElementText();
a.getItems().add(item);
}
我其实在思考:
为什么你选择了Stax而不是JAXB?实际上,你的XML架构非常简单,可以使用JAXB处理。将XML转换为Java对象的代码几乎没有样板代码,这意味着使用JAXB,你的代码将更可读和易于维护。这只是我的个人观点。
英文:
First, your xml is missing the Person element closing just before the end.
</Person>
Second, your Person class is not really correct. Items should be a list of String instead of just a String.
public class Person {
// private keyword is removed for brevity
String name;
List<String> items = new ArrayList<>();
}
Third, your code needs to be updated to use the list of items as follows:
if (elementName.equals("Item"))
{
String item= reader.getElementText();
a.getItems().add(item);
}
I'm actually wondering:
Why do you choose Stax instead of JAXB? Your XML schema is actually quite straight-forward to be handled with JAXB. And there will be very little boilerplate code for the conversion of XML to Java object, which means your code will be more readable and maintainable with JAXB. Just my 2 cents.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论