Example of a method manipulating a document DOM tree (see Homework 1):
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Uloha1 {
Document doc;
/**
* ***********************************************************************
* Method for a salary modification. If the person's salary is less then
* <code>minimum</code>, the salary will increased to
* <code>minimum>.
* No action is performed with the rest of persons.
*/
public void adjustSalary(double minimum) {
// get the list of salaries
NodeList salaries = doc.getElementsByTagName("salary");
for (int i = 0; i < salaries.getLength(); i++) {
// get the salary element
Element salaryElement = (Element) salaries.item(i);
// get payment
double salary = Double.parseDouble(salaryElement.getTextContent());
if (salary < minimum) {
// modify the text node/content of element
salaryElement.setTextContent(String.valueOf(minimum));
}
}
}
}