Java 如何将节点从一个文档导入到另一个文档而不附加
我需要使用深度副本(属性、ns信息等)将节点从一个文档(doc1)注册到另一个文档(doc2),以便修复Java8中的getElementById问题。 因此,我只需要在doc2中注册这些节点,而不需要将它们附加到父节点中。adoptNode和importNode的文档API要求将目标节点附加到doc2中,这会修改doc2的dom结构。是否有任何方法可以注册节点而不附加Java 如何将节点从一个文档导入到另一个文档而不附加,java,xml,dom,Java,Xml,Dom,我需要使用深度副本(属性、ns信息等)将节点从一个文档(doc1)注册到另一个文档(doc2),以便修复Java8中的getElementById问题。 因此,我只需要在doc2中注册这些节点,而不需要将它们附加到父节点中。adoptNode和importNode的文档API要求将目标节点附加到doc2中,这会修改doc2的dom结构。是否有任何方法可以注册节点而不附加 public void testAddNodesToDocument() throws Exception { Doc
public void testAddNodesToDocument() throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
InputStream payloadData = new ByteArrayInputStream( "<Wrapper><TagA Id=\"attributeId\" Target=\"SomeTarges\">test</TagA></Wrapper>".getBytes() );
Document doc = builder.parse( payloadData );
setAttributeId( doc );
assertNotNull( doc.getElementById( "attributeId" ) );
Document newDoc = builder.newDocument();
Element nodeList = doc.getDocumentElement();
Node result = newDoc.importNode( nodeList, true );
// if appendChild is commnented out, the assertion of getElementById fails
// newDoc.appendChild( result );
setAttributeId(newDoc);
assertNotNull( "getElementsByTagName doesn't work, element is not imported!", newDoc.getElementsByTagName( "TagA" ) );
assertNotNull( "getElementById doesn't work!", newDoc.getElementById( "attributeId" ) );
}
private void setAttributeId( Document doc )
{
NodeList list = doc.getElementsByTagName( "*" );
for( int i = 0; i < list.getLength(); i++ )
{
Node node = list.item( i );
if( node.getNodeType() == Node.ELEMENT_NODE )
{
// do something with the current element
org.w3c.dom.Element item = (Element)node;
Attr idAttr = item.getAttributeNode( "Id" );
if( idAttr != null )
{
item.setIdAttributeNode( idAttr, true );
}
}
}
}
public void testAddNodesToDocument()引发异常
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder=dbf.newDocumentBuilder();
InputStream payloadData=new ByteArrayInputStream(“test”.getBytes());
Document doc=builder.parse(payloadData);
setAttributeId(doc);
assertNotNull(doc.getElementById(“attributeId”);
Document newDoc=builder.newDocument();
元素nodeList=doc.getDocumentElement();
节点结果=newDoc.importNode(nodeList,true);
//如果将appendChild输入,则getElementById的断言将失败
//newDoc.appendChild(结果);
setAttributeId(newDoc);
assertNotNull(“getElementsByTagName不工作,元素未导入!”,newDoc.getElementsByTagName(“TagA”);
assertNotNull(“getElementById不工作!”,newDoc.getElementById(“attributeId”);
}
私有void setAttributeId(文档文档)
{
节点列表=doc.getElementsByTagName(“*”);
对于(int i=0;i
我不明白为什么要使用Node importedNode=doc2.importNode(node1,true)代码>涉及任何附加到父节点的操作。我也不明白“注册节点”到底是什么意思。也许您可以用一个代码片段来解释您所做的事情、您收到的错误消息、您想要的结果以及您当前得到的结果。@MartinHonnen是的,我更新了示例代码。我希望getElementById也能在doc2中工作,而不会破坏原始Doc2DOM结构。好吧,newDoc.getElementById
在newDoc
树中按id查找元素,如果树中没有这样的元素,为什么getElementById
要查找它们?如果您没有将节点附加到树,那么文档。getElementById
找不到它们,无论您使用的是两个不同的文档和导入节点还是单个文档都不重要。因此,我不确定您想要实现什么或期望getElementById
做什么,您希望它返回特定文档所拥有的id元素吗?事实并非如此。@MartinHonnen好吧,我这样做的根本原因超出了这个主题,如果它有助于你理解背景,我需要它在XML数字签名中工作,将一些节点合并到一个原始文档中,其中包含应该签名的数据,这就是为什么我不能破坏原始dom结构。我试着从doc1创建单个元素,但没有将元素附加到doc1,它可以工作。我正在寻找将节点合并到doc1中的可能性,以获得创建单个元素的相同结果。应该通过importNode
实现“不破坏原始doc2 dom结构”部分。