【问题标题】:Read multiple child of Element in XML using STAX JAVA使用 STAX JAVA 在 XML 中读取 Element 的多个子项
【发布时间】:2020-08-01 23:54:34
【问题描述】:

如何读取 Item 元素的所有子项并将其存储在对象中

这是我的示例 xml 文件

<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>

我用 getter 和 setter 创建了一个 Person 对象

public class Person{ // SAMPLE OBJECT
    private String name; @getters and setters 
    private String item; @getters and setters
}

这是我的读取方法

  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;
    }


此方法显示
本
亚历克斯

但如果我只使用类似代码获取项目,它会显示
书籍
橡皮擦

    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());
    }

我想做这个输出

Ben
    Pen
    Paper
    Book
Alex
    Pencil 
    Eraser

【问题讨论】:

    标签: java xml element xmlreader stax


    【解决方案1】:

    首先,您的 xml 缺少在结束前关闭的 Person 元素。

    </Person>
    

    其次,您的 Person 类并不正确。项目应该是一个字符串列表,而不仅仅是一个字符串。

    public class Person {
    // private keyword is removed for brevity
        String name; 
        List<String> items = new ArrayList<>(); 
    }
    

    第三,您的代码需要更新以使用项目列表如下:

    if (elementName.equals("Item"))
    {
        String item= reader.getElementText();
        a.getItems().add(item);
    }
    

    我其实想知道: 为什么选择 Stax 而不是 JAXB?使用 JAXB 处理您的 XML 模式实际上非常简单。并且将很少有用于将 XML 转换为 Java 对象的样板代码,这意味着您的代码使用 JAXB 将更具可读性和可维护性。只是我的 2 美分。

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 2011-06-06
      • 2015-07-17
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 2015-12-29
      • 2011-11-05
      • 2012-03-11
      相关资源
      最近更新 更多