本文介绍了在Java中解析XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个与此相似的xml
I have an xml similar to this
<Applications>
<ApplicationID>
<VendorId value="0" />
<AuthApplId value="4" />
<AcctApplId value="0" />
</ApplicationID>
<ApplicationID>
<VendorId value="193" />
<AuthApplId value="0" />
<AcctApplId value="19302" />
</ApplicationID>
</Applications>
我想解析这个并存储到Strings Like VendorId,AuthApplId等我得到了ApplicationID解析为 getElementsByTagName(ApplicationID)
如果它是< ApplicationID value =somevalue/>
那么我可以使用 getAttribute(value)
方法。但是在上述情况下我应该怎么做?
I want to parse this and store to Strings Like VendorId, AuthApplId etc. I got ApplicationID parsed with getElementsByTagName("ApplicationID")
if it was <ApplicationID value="somevalue"/>
then I can use getAttribute("value")
method. but in the above mentioned situation what should I do?
推荐答案
NodeList applicationIDNodes = getElementsByTagName("ApplicationID");
for (int i = 0; i < applicationIDNodes.length; i++) {
Node applicationIDNode = applciationIdNodes.getItem(i);
NodeList applicationIdChildren = applicationIdNode.getChildren();
String vendorId = applicationIdChildren.getItem(0).getAttribute("value").value();
String authAppliId = applicationIdChildren.getItem(1).getAttribute("value").value();
String actApplID = applicationIdChildren.getItem(2).getAttribute("value").value();
// do whathever you want with vendorId, authAppliId, actApplID
}
这篇关于在Java中解析XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!