我想使用dom修改xml文件,但是當我創建node.getNodeValue();它返回null!我不知道爲什麼?我的XML文件包含以下標籤: [person] which contains child [name] which contains childs [firstname ,middleInitial ,lastName] childs
XML Dom處理
我想更新的名字,middleInitial和使用DOM 姓氏,這是我的Java的DOM處理文件:
NodeList refPeopleList = doc.getElementsByTagName("person");
for (int i = 0; i < refPeopleList.getLength(); i++) {
NodeList personList = refPeopleList.item(i).getChildNodes();
for (int personDetalisCnt = 0; personDetalisCnt < refPeopleList.getLength(); personDetalisCnt++) {
{
currentNode = personList.item(personDetalisCnt);
String nodeName = currentNode.getNodeName();
System.out.println("node name is " + nodeName);
if (nodeName.equals("name")) {
System.out.println("indise name");
NodeList nameList = currentNode.getChildNodes();
for(int cnt=0;cnt<nameList.getLength();cnt++)
{
currentNode=nameList.item(cnt);
if(currentNode.getNodeName().equals("firstName"))
{
System.out.println("MODIFID NAME :"+currentNode.getNodeValue()); //prints null
System.out.println("indide fname"+" node name is "+currentNode.getNodeName()); //prints firstName
String nodeValue="salma";
currentNode.setNodeValue(nodeValue);
System.out.println("MODIFID NAME :"+currentNode.getNodeValue());//prints null
}
}
}
}
你可以寫一個簡單的助手類來完成你的任務。看到這個http://stackoverflow.com/a/8346867/851432 – Jomoos 2011-12-01 19:17:10