当前位置: 技术问答>java相关
如何在一个DOM中更新一个节点或者一部分节点
来源: 互联网 发布时间:2015-09-15
本文导语: 写了一个程序,想在一个DOM dataDoc中,更新一部分节点,但是输出结果不对 请教各位高手 // 程序清单 package test; import java.io.*; import java.sql.*; import java.util.*; import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xm...
写了一个程序,想在一个DOM dataDoc中,更新一部分节点,但是输出结果不对
请教各位高手
// 程序清单
package test;
import java.io.*;
import java.sql.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class UpdateDocument
{
public static void main(String[] args)
{
Document dataDoc = null;
Element dataRoot = null;
Document updateDoc = null;
Element updateRoot = null;
try
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
dataDoc = docBuilder.newDocument();
dataRoot = dataDoc.createElement("root");
Element aNode = dataDoc.createElement("AAA");
Element bNode = dataDoc.createElement("BBB");
dataRoot.appendChild(aNode);
dataRoot.appendChild(bNode);
dataDoc.appendChild(dataRoot);
updateDoc = docBuilder.newDocument();
updateRoot = updateDoc.createElement("CCC");
updateDoc.appendChild(updateRoot);
dataDoc.getDocumentElement().removeChild(aNode);
System.out.println(dataDoc.getDocumentElement().toString());
dataDoc.getDocumentElement().appendChild(updateDoc);
System.out.println(dataDoc.getDocumentElement().toString());
}
catch (Exception ex)
{
}
}
}
// 输出结果如下:
我不理解,为什么后面appendChild(updateDoc)后,什么都打印不出来了呢?
请高手指点,谢谢
请教各位高手
// 程序清单
package test;
import java.io.*;
import java.sql.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class UpdateDocument
{
public static void main(String[] args)
{
Document dataDoc = null;
Element dataRoot = null;
Document updateDoc = null;
Element updateRoot = null;
try
{
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
dataDoc = docBuilder.newDocument();
dataRoot = dataDoc.createElement("root");
Element aNode = dataDoc.createElement("AAA");
Element bNode = dataDoc.createElement("BBB");
dataRoot.appendChild(aNode);
dataRoot.appendChild(bNode);
dataDoc.appendChild(dataRoot);
updateDoc = docBuilder.newDocument();
updateRoot = updateDoc.createElement("CCC");
updateDoc.appendChild(updateRoot);
dataDoc.getDocumentElement().removeChild(aNode);
System.out.println(dataDoc.getDocumentElement().toString());
dataDoc.getDocumentElement().appendChild(updateDoc);
System.out.println(dataDoc.getDocumentElement().toString());
}
catch (Exception ex)
{
}
}
}
// 输出结果如下:
我不理解,为什么后面appendChild(updateDoc)后,什么都打印不出来了呢?
请高手指点,谢谢
|
you can get the tag name of the oDoc.oRoot, then use dataDoc.getElementsByTagName to get those nodes in dataDoc and remove them,...
|
1. removeChild must be called on a direct parent
2. you cannot just add a node from one document to another document, you need to use importNode
see the errors with
catch (Exception ex)
{
ex.printStackTrace();
}
2. you cannot just add a node from one document to another document, you need to use importNode
see the errors with
catch (Exception ex)
{
ex.printStackTrace();
}