我的 XML 文件采用以下格式:
<top>
  <name></name>
  <title></title>
  <time></time>
</top>
<top>
  ...
</top>
<top>
  ...
</top>
我编写以下代码来读取 xml 文件:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(QUERY_FILE));  //LINE (*)
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("top");
但问题是我在 (*) 行遇到错误:
文档中根元素之后的标记必须格式正确。
似乎该错误是因为我在 xml 文件中有多个根元素。一种解决方案可能是我<doc></doc>在所有<top>元素之外添加。但是有没有其他方法可以直接在 XML 文件中读取元素数组?