您好我需要将节点从一个XML复制到另一个XML:
String _sPathOfrObj = "/response/grp/ofr/obj";
String _sPathHotelData = "/main/hotelData";
String _sPathStatus = "/main/status";
xpath.reset();
NodeList status = (NodeList) xpath.evaluate( _sPathStatus, details, XPathConstants.NODESET );
if(status.item( 0 ).getTextContent().equalsIgnoreCase( "ok" ))
{
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
{
nodes3.item( i ).appendChild( nodes4.item( i ) );
}
catch(DOMException dome)
{
dome.printStackTrace();
Log.e(CLASS_NAME, dome.code+"" );
}
}
}
但它会生成错误,DOMException为WRONG_DOCUMENT_ERR,或者当我执行nodes4.item( i ).deepClone(true or false)
时,它将抛出DOMException NAMESPACE_ERR
有办法吗?
非常感谢
答案 0 :(得分:1)
我已经设法让它工作,它是我的解决方案的混合物,克服了深度抛出NAMESPACE_ERR和link中提出的解决方案。
基本上我无法在没有错误的情况下制作给定节点的副本,因此我将XML扁平化为字符串并将字符串转换为新节点并使用adoptNode我有我的解决方案:)
我确实知道这是一个黑客的地狱,但它适用于我紧迫的截止日期:)
转换为字符串是通过使用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;
}
并将其转换回Node
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是为了满足最后期限,如果有人会提出简洁的解决方案,我会很乐意坚持:)