一次读取xml元素的所有属性

时间:2013-11-07 11:34:00

标签: java xml dom

我有xml文件,如下所示

<dashboard  DASHBOARD_ID="1" DASHBOARD_IMAGE="" DASHBOARD_NAME="TestDashboard">
<linkedpages>
<pages page_id=1212 pagename=""/>
<report reportid=212 reportname=""/>
</linkedpages>

我的需要是我应该将这些标记属性velues导入到相应的表中,例如页表,报表,dashborad表等。

我通过

获取元素及其属性
String attribute = child.getAttribute("report_id");

但是我需要编写n个这样的行,而且它不是通用的,我可以有可变长度的属性。 所以我需要能够读取每个标签的所有属性。

如何做到这一点请帮助,任何想法这样做都表示赞赏。 谢谢

2 个答案:

答案 0 :(得分:1)

    String[] attributes = new String[child.getAttributes().getLength()];
    for (int i = 0; i < attributes.length; i++) {
        attributes[i] = child.getAttributes().item(i).getNodeValue();
    }

答案 1 :(得分:1)

尝试此方法:getAttributes()

一个例子:

List<String> attributNames = new ArrayList<String>();
if(child.getAttributes() != null){
    for (int i = 0; i < child.getAttributes().getLength(); i++) {
        attributNames.add(child.getAttributes().item(i).getNodeName());
    }
}
相关问题