Bom dia, pessoal
Tenho o seguinte arquivo XML (arquivo.xml) que gostaria de alterar:<html>
<head>
<title>Título</title>
</head>
<body onload="javascript:alert('Olá!')">
<p>Olá Mundo!</p>
</body>
</html>
public class Exemplo {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(new File("arquivo.xml"));
// mudando o valor de 'title'
Element htmlTag = doc.getDocumentElement();
Element headTag = (Element) htmlTag.getElementsByTagName("head")
.item(0);
Element titleTag = (Element) headTag.getElementsByTagName("title")
.item(0);
titleTag.setTextContent("Novo título");
}
}