You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by Keith Reynolds <ch...@yahoo.com> on 2005/11/23 18:04:26 UTC
XercesDOMSupport: Converting between Xalan nodes and Xerces DOM nodes for removal
Hello,
I have seen some other postings with similar questions. In general
there was agreement to work up a sample but none exists - that I can
find.
I would like to use Xalan for XPATH searching and then allow removal
of some nodes. I have tried using the wrapper and haven't had any
success. The wrapper's function removeChild() fails with
NO_MODIFICATION_ALLOWED_ERR and I apparently can't call on the
XercesNode::removeChild() - because it's returned 'const' from
mapNode().
I have the code shown below.
As an alternative approach (one that I less favor) could the selected
nodes be passed into a XSLT transformation to achieve the same result?
Thanks,
Keith Reynolds
<code>
// Using Xalan-C 1.10
// Using Xerces-C 2.7
XALAN_USING_XALAN(FormatterToXercesDOM)
XALAN_USING_XALAN(NodeRefList)
XALAN_USING_XALAN(XPathEvaluator)
XALAN_USING_XALAN(XalanDOMString)
XALAN_USING_XALAN(XalanDocument)
XALAN_USING_XALAN(XalanDocumentPrefixResolver)
XALAN_USING_XALAN(XalanNode)
XALAN_USING_XALAN(XercesDOMSupport)
XALAN_USING_XALAN(XercesDOMWrapperParsedSource)
XALAN_USING_XALAN(XercesDocumentWrapper)
XALAN_USING_XALAN(XercesParserLiaison)
XALAN_USING_XERCES(MemBufInputSource);
XALAN_USING_XALAN(XalanElement)
XALAN_USING_XERCES(DOMNode)
bool retval = false;
char szXML[] =
"<DET><Filelist><File><ID>001</ID><Filename>\\\\server\\workspace\\file1.wav</Filename></File><File><ID>002</ID><Filename>\\\\server\\workspace\\file2.wav</Filename></File></Filelist></DET>";
int nSize = sizeof(szXML);
MemBufInputSource memBuf((const XMLByte*)szXML, nSize, "memory_buffer",
false);
XercesDOMSupport theDOMSupport;
XercesParserLiaison theLiaison(theDOMSupport);
theLiaison.setBuildWrapperNodes(true);
theLiaison.setBuildMaps(true);
XalanDocument* xalan_document = theLiaison.parseXMLStream(memBuf);
assert(xalan_document != 0);
XercesDocumentWrapper* docWrapper =
theLiaison.mapDocumentToWrapper(xalan_document);
assert(docWrapper != 0);
XalanNode * contextNode = xalan_document;
assert(contextNode != 0);
XalanElement * namespace_node = xalan_document->getDocumentElement();
assert(namespace_node != 0);
XPathEvaluator theEvaluator;
XalanNode * xalan_node = theEvaluator.selectSingleNode(
theDOMSupport,
contextNode,
XalanDOMString("DET//File[ID = '001']").c_str(),
XalanDocumentPrefixResolver(docWrapper)
);
if (xalan_node)
{
XalanDOMString nodeName = xalan_node->getNodeName();
std::cout << "Found node" << std::endl;
const DOMNode * xerces_node = docWrapper->mapNode(xalan_node);
//////////////////////////////////////////////////////////////////////////
// If I try this, the exception 'NO_MODIFICATION_ALLOWED_ERR' is
thrown
docWrapper->removeChild(xalan_node);
//////////////////////////////////////////////////////////////////////////
if (NULL != xerces_node)
{
const XMLCh * pName = xerces_node->getNodeName();
DOMNode* xerces_parent = xerces_node->getParentNode();
if (NULL != xerces_parent)
{
//////////////////////////////////////////////////////////////////////////
// this call is not valid because child is 'const'
xerces_parent->removeChild(xerces_node);
//////////////////////////////////////////////////////////////////////////
}
}
}
// todo: return the modified DOM in some manner - MemBuf?
// todo: how do we reload the wrapper if necessary?
</code>
__________________________________
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com