package pars;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class Class1 {
public Class1() {
}
public static void main(String[] args) {
Class1 class1 = new Class1();
File docFile = new File("D:\\Krapuzik\\xmldoc03.xml");
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
} catch (Exception e) {
// TODO
}
Element root = doc.getDocumentElement();
System.out.println(root);
NodeList children = root.getChildNodes();
System.out.println(children.getLength());
// class1.stepThrough(root);
changeOrder(root, "dogName", "ing");
NodeList orders = root.getElementsByTagName("dogName");
for (int orderNum = 0;
orderNum < orders.getLength();
orderNum++)
{
System.out.println(orders.item(orderNum)
.getFirstChild().getNodeValue());
}
}
private static void changeOrder (Node start,
String elemName,
String elemValue)
{
if (start.getNodeName().equals(elemName)) {
start.getFirstChild().setNodeValue(elemValue);
}
for (Node child = start.getFirstChild();
child != null;
child = child.getNextSibling())
{
changeOrder(child, elemName, elemValue);
}
}
private static void stepThrough (Node start){
System.out.println(start.getNodeName()+" = "+start.getNodeValue());
for (Node child = start.getFirstChild();child != null;child = child.getNextSibling())
{
if (start.getNodeName().equalsIgnoreCase("dogName")){
}
stepThrough(child);
}
}
}