使用[java]sax读取xml文件的最佳算法/模式是什么?
假设我们有以下xml使用[java]sax读取xml文件的最佳算法/模式是什么?,java,xml,algorithm,design-patterns,Java,Xml,Algorithm,Design Patterns,假设我们有以下xml <books> <book> <id>...</id> <title>...</title> <references> <book> <id>...</id> <title>...</title> <
<books>
<book>
<id>...</id>
<title>...</title>
<references>
<book>
<id>...</id>
<title>...</title>
</book>
</references>
<authors>
<author>
<id>...</id>
<name>...</name>
<written-books>
<book>
<id>...</id>
<title>...</title>
</book>
</written-books>
</author>
</authors>
<book>
</books>
但它似乎不太优雅。有更好的解决方案吗?据我所知,您需要做的是读取XML,然后对其进行处理。(检查元素是否存在等)您可以使用ApacheAxiom读取xml文件并轻松获取内容。然后您可以根据需要处理它。我将使用AXIOM提供一个示例代码段来获取xml文件的内容
//read the xml file
StAXOMBuilder builder = new StAXOMBuilder(xmlStreamReader);
OMElement endPointElem = builder.getDocumentElement();
// go though the xml elemetns and do whatever you want
Iterator children = endPointElem.getChildElements();
while (children.hasNext()) {
........................
.......
}
有关详细信息谢谢您抽出时间。我认为我的问题在于使用sax而不是stax。
//read the xml file
StAXOMBuilder builder = new StAXOMBuilder(xmlStreamReader);
OMElement endPointElem = builder.getDocumentElement();
// go though the xml elemetns and do whatever you want
Iterator children = endPointElem.getChildElements();
while (children.hasNext()) {
........................
.......
}