所以我想从 xml 文件读取数据,我现在已经有了 XMLReader
和 ContentHandler
,并且当 endDocument()
被触发时,我已经从文档中“收集”了我需要的所有数据。
但现在看来我碰壁了......
如何返回收集的数据(从 ContentHandler
)以便可以在我的应用程序中使用它?
最佳答案
您可以创建 List<T>
在内容处理程序中。
public class MyTextHandler implements ContentHandler {
....
private ArrayList<YourModel> list;
public MyTextHandler() {
list= new ArrayList<YourModel>();
}
public ArrayList<YourModel> getList() {
return list;
}
....
}
从处理程序获取列表:
MyTextHandler handler=new MyTextHandler();
reader.setContentHandler(handler);
InputSource is = new InputSource(filename);
reader.parse(is);
ArrayList<YourModel> list=handler.getList();
关于java - 返回解析 XML 文档时创建的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499273/