我已经设法让它工作,它是我克服 deepClone 抛出 NAMESPACE_ERR 的解决方案和链接中提供的解决方案的混合体。
基本上我无法在没有错误的情况下制作给定节点的副本,因此我将 XML 展平为字符串并将字符串转换为新节点,并通过使用采用节点我有我的解决方案:)
我确实意识到这是一个黑客,但它适用于我紧迫的截止日期:)
转换为字符串是通过使用 Transformer 类完成的,如下所示:
private static final String CLASS_NAME = "SendQueryTask";
//...
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
//...
private String transformXmlToString(Node node)
{
Transformer transformer = null;
try
{
transformer = TransformerFactory.newInstance().newTransformer();
}
catch (TransformerConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (TransformerFactoryConfigurationError e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if ( transformer != null )
{
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
// initialize StreamResult with File object to save to file
StreamResult result = new StreamResult( new StringWriter() );
DOMSource source = new DOMSource( node );
try
{
transformer.transform( source, result );
}
catch (TransformerException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String xmlString = result.getWriter().toString();
Log.i( CLASS_NAME, "flattened=" + (xmlString) );
return xmlString;
}
return null;
}
并将其转换回节点
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.protocol.HTTP;
import java.io.UnsupportedEncodingException;
private Node dumbNodeCopy(Node item)
{
String xmlString = transformXmlToString( item );
if ( xmlString != null )
{
InputStream is = null;
try
{
is = new ByteArrayInputStream( xmlString.getBytes( HTTP.UTF_8 ) );
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
if ( is != null )
{
Document doc = null;
dbf = DocumentBuilderFactory.newInstance();
try
{
db = dbf.newDocumentBuilder();
doc = db.parse( is );
doc.getDocumentElement().normalize();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
if ( doc != null ) return doc.getFirstChild();
}
return null;
}
return null;
最后是复制
NodeList nodes3 = (NodeList) xpath.evaluate( _sPathOfrObj, result,
XPathConstants.NODESET );
NodeList nodes4 = (NodeList) xpath.evaluate( _sPathHotelData, details,
XPathConstants.NODESET );
minimum = Math.min( nodes3.getLength(), nodes4.getLength() );
Log.i( CLASS_NAME, "obj=" + nodes3.getLength() );
Log.i( CLASS_NAME, "hdat=" + nodes4.getLength() );
for (i = 0; i < minimum; i++)
{
try
{
Node copy = dumbNodeCopy( nodes4.item( i ) );
if ( copy != null )
{
// nodes3.item( i ).appendChild( copy );
nodes3.item( i ).appendChild(
nodes3.item( i ).getOwnerDocument().adoptNode( copy ) );
}
else
Log.e( CLASS_NAME, "Copy of nodes4#" + i + ", failed." );
}
catch (DOMException dome)
{
dome.printStackTrace();
Log.e( CLASS_NAME, dome.code + "" );
}
}
顺便提一句。不要嘲笑代码:-) 我知道它看起来很糟糕,但我的必要 ATM 是在最后期限之前完成,如果有人能提出简洁的解决方案,我会很乐意遵守:)