如何使用getElementsByTagName获取值 [英] How to get the value using getElementsByTagName
本文介绍了如何使用getElementsByTagName获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用getElementsByTagName获取标签名称的值.我的Xml文件是
How to get the value of the tag name using getElementsByTagName. My Xml file is
<parent>
<method>name</method>
....
....
</parent>
在这里,我想单独考虑方法的价值.我使用了下面的代码,但是我得到了对象
Here i want to take the value of method alone. i used the following piece of code, but i am getting as object
File fXmlFile = new File(FILE_XML);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getElementsByTagName("method").toString();
推荐答案
doc.getElementsByTagName("method")
返回您需要第一个,因此您应该使用 doc.getElementsByTagName("method").item(0)
-返回
You want the first one of these, so you should use doc.getElementsByTagName("method").item(0)
- which returns a Node
.
由此,您可能需要该值; doc.getElementsByTagName("method").item(0).getTextContent()
应该可以帮助您.
From this, you probably want the value; doc.getElementsByTagName("method").item(0).getTextContent()
should get you that.
这篇关于如何使用getElementsByTagName获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文