如何在xml dom解析器中获取节点列表的值? [英] How to get value of node list in xml dom parser?
问题描述
我有一个像这样的 XML ::
I have one XML which look like this ::
<Channels>
<Channel Id="511" Title="Test" ChannelDescription="This is Test Channel./>
</Channels>
我成功解析了这种 XML.我的问题是,当我启动 webservice 时,如果没有来自服务器的身份验证,那么 webservice 响应如下::
I am successfully parse this kind of XML.My Problem is that when i fired the webservice and if there is no authentication from the server then the webservice response like this::
<AuthenticationError>An Active Session Already Exists For This User.</AuthenticationError>
那么我如何检查根节点是身份验证错误"还是注释".如果我得到身份验证错误标签,那么我怎样才能得到它的节点值,即此用户的活动会话已经存在."??
So how can i check that root node is "Authentication Error" or "Notes". And if i get the Authentication Error tag then how can i get its node value which is "An Active Session Already Exists For This User."??
XML 解析代码是这样的::
Code for XML Parsing is this::
NodeList node =null;
node= (NodeList)result.getElementsByTagName("Channels");
for(int j=0;j<node.getLength();j++)
{
Node aNode=node.item(j);
Element fstElmnt = (Element) aNode;
NodeList websiteList = fstElmnt.getElementsByTagName("Channel");
int check=websiteList.getLength();
for(int k=0;k<check;k++)
{
DatabaseConstant myChannels = new DatabaseConstant();
Node checkNode=websiteList.item(k);
Element websiteElement = (Element) checkNode;
myChannels.id=websiteElement.getAttribute("Id");
myChannels.title=websiteElement.getAttribute("Title");
channel .add(myChannels);
}
}
}
我希望我的问题很清楚...请尽快提供解决方案.提前致谢....
I hope my question is clear... Please provide the solution asap. Thanks in Advance....
推荐答案
使用 getDocumentElement()
获取 root Element
,然后使用 getTagName()
代码>获取标签名称.
Use getDocumentElement()
to get root Element
, then use getTagName()
to get tag name.
示例:
Element root = result.getDocumentElement();
String name = root.getTagName();
if(name.equalsIgnoreCase("AuthenticationError") )
{
String value = myDocument.getDocumentElement().getTextContent();
System.out.println("Error:" + value);
}
else if(name.equalsIgnoreCase("Notes") )
{
NodeList nodes = root.getElementsByTagName("Channels");
for(int i = 0 ; i < nodes.getLength() ; i ++)
{
//-----do something with channels nodes--
}
}
这篇关于如何在xml dom解析器中获取节点列表的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!