Pergunta

I need to remove all attributes from a particular element in a Document.

How I have the xml :

    <Book>
     <Chapter TotalNoOfWords="2000" NoOfPages="5">
       <Line LineNo="1" NumberOfWords="50"/>
     </Chapter>
    </Book>

How I need it to be :

    <Book>
     <Chapter>
      <Line LineNo="1" NumberOfWords="50"/>
     </Chapter>
    </Book>

I am aware of the removeAttribute() method. But this method will only remove the attribute for which the name has been passed as an argument to this method. I am looking for something that will remove all attributes in the element. Any help on this.

Foi útil?

Solução

Assuming you already have the specified Node element, loop through all the elements and remove them...

while (node.getAttributes().getLength() > 0) {
    Node att = node.getAttributes().item(0);
    node.getAttributes().removeNamedItem(att.getNodeName());
}

For example...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top