获取dom节点的属性 [英] Get an attribute of a dom node

查看:146
本文介绍了获取dom节点的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取xml节点示例的属性:

I am trying to get an attribute of an xml node example:

<Car name="Test">
</Car>

我想获取汽车节点的name属性。

I want to grab the name attribute of the car node.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();          
Document doc = db.parse(configFile);
doc.getDocumentElement().normalize();           
NodeList layerConfigList = doc.getElementsByTagName("CAR");
Node node = layerConfigList.item(0);
// get the name attribute out of the node.

这是我卡住的地方,因为我可以使用的唯一方法是getAttributes()with返回一个NamedNodeMap,我不知道如何从中提取它。

this is where i get stuck because the only method that looks like i can use is getAttributes() with returns a NamedNodeMap and im not sure how to extract it from that.

推荐答案

你的节点是一个元素,所以你只需要

Your node is an Element so you just have to

Element e = (Element)node;
String name = e.getAttribute("name");

这篇关于获取dom节点的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆