⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e537. removing all the attributes in a dom element.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
The tricky part of removing all the attributes in an XML element is to realize that attributes with a default value cannot be removed. Therefore, the following code will never terminate if the element has an attribute with a default value: 
    // Very bad code; may never terminate
    NamedNodeMap attrs = element.getAttributes();
    while (attrs.getLength() > 0) {
        attrs.removeNamedItem(attrs.item(0).getNodeName());
    }

This example removes all attributes by first making a copy of the attribute names and then using the list to remove the attributes: 
    // Remove all the attributes of an element
    NamedNodeMap attrs = element.getAttributes();
    String[] names = new String[attrs.getLength()];
    for (int i=0; i<names.length; i++) {
        names[i] = attrs.item(i).getNodeName();
    }
    for (int i=0; i<names.length; i++) {
        attrs.removeNamedItem(names[i]);
    }

 Related Examples 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -