如何使用 dom4j 迭代 Xml
我的xml文件:
<credentials>
<machine name="xyz">
<cred-pairs>
<cred-pair>
<login>asad</login>
<password>12345</password>
</cred-pair>
<cred-pair>
<login>ggss</login>
<password>97653</password>
</cred-pair>
<cred-pairs>
</machine>
<machine name="pqr">
<cred-pair>
<cred-pair>
<login>ssdas</login>
<password>12345</password>
</cred-pair>
<cred-pairs>
</machine>
</credentials>
客户端:
public Client
{
String login;
String password;
//getters
Client(String login,String password)
{
this.login=login;
this.password=password;
}
}
我的测试类:
Class Test{
getMachineByName(String machineName)
{
ArrayList<Client> machineClients=new ArrayList<Client>();
/*here i have to iterate through xml and upon machineName i have to create Client objects using cred-pair(s) in cred-pairs node and add to machineClientsList
}
}
如果我调用getmachineByName(xyz)
,我应该得到所有数组列表中的信用对。我在迭代中感到困惑。
my xml file:
<credentials>
<machine name="xyz">
<cred-pairs>
<cred-pair>
<login>asad</login>
<password>12345</password>
</cred-pair>
<cred-pair>
<login>ggss</login>
<password>97653</password>
</cred-pair>
<cred-pairs>
</machine>
<machine name="pqr">
<cred-pair>
<cred-pair>
<login>ssdas</login>
<password>12345</password>
</cred-pair>
<cred-pairs>
</machine>
</credentials>
Client :
public Client
{
String login;
String password;
//getters
Client(String login,String password)
{
this.login=login;
this.password=password;
}
}
My Test Class:
Class Test{
getMachineByName(String machineName)
{
ArrayList<Client> machineClients=new ArrayList<Client>();
/*here i have to iterate through xml and upon machineName i have to create Client objects using cred-pair(s) in cred-pairs node and add to machineClientsList
}
}
if i call getmachineByName(xyz)
, i should get all the cred-pairs in a arraylist. I am confused in iteration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的方法可能是使用 XPATH,类似这样的东西。我假设您使用的是 Dom4j 1.6.1。
The best way is probably with XPATH, with something like this. I'm assuming that you are using Dom4j 1.6.1.