使用xPath从XML创建java树对象



所以,我的问题是:我想使用xPath表达式和算法解析XML文件,以便创建我的对象(List<Team>(。

我的类定义如下:

public class Team {
private String itemId;
private String name;
private boolean archived;
private List<Team> children;
public Team(String itemId, String name, boolean archived) {
this.children = new ArrayList<TeamArea>();
this.itemId = itemId;
this.name = name;
this.archived = archived;
}
// getters and setters
}

这里是XML结构的一个示例:(子节点可以有0到n个子节点(

<children>
<itemId>first team</itemId>
<name>first team</name>
<archived>first team</archived>
<children>
<itemId>first child of first team</itemId>
<name>first child of first team</name>
<archived>first child of first team</archived>
</children>
</children>
<children>
<itemId>second team</itemId>
<name>second team</name>
<archived>second team</archived>
<children>
<itemId>first child of second team</itemId>
<name>first child of second team</name>
<archived>first child of second team</archived>
<children>
<itemId>first child of child above</itemId>
<name>first child of child above</name>
<archived>first child of child above</archived>
</children>
</children>
</children>

我使用org.w3.dom.Node和NodeList接口。我尝试使用xPath表达式://children/node((,但我无法浏览NodeList来创建我的List<团队>对象

有人能帮我使用算法和xpath表达式吗?

谢谢!

从XPath:开始

//children[not(parent::children)]

然后foreach儿童使用XPath:

children

最新更新