![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: replacement of node from string [Thread Next] Re: replacement of node from stringTo: NULL Date: 4/4/2005 10:10:00 PM Thanks Martin,
It took me a little while, but I finally managed to try what you suggested,
but it still ASSERTS. I'm working with C++ smart pointers, and this is what
I tried. If someone could suggest why the replaceChild asserts, that would
be very appreciated. Essentially, I'm trying to replace a node with another
node that starts as a string.
HRESULT ParseSnippetIntoFrag( MSXML::IXMLDOMDocumentPtr spRoot, CString&
strNewXML, MSXML::IXMLDOMDocumentFragmentPtr& spFragment )
{
HRESULT hr = S_OK;
CString CSValue;
MSXML::IXMLDOMDocumentPtr spNewDoc = NULL;
hr = spNewDoc.CreateInstance( XML_DOM_PROGID, //
"Msxml2.DOMDocument.3.0"
NULL,
CLSCTX_INPROC_SERVER );
ASSERT( SUCCEEDED( hr ) );
spNewDoc->async = false;
CSValue = _T( "<dummy>" ) + strNewXML + _T( "</dummy>" );
if ( spNewDoc->loadXML( ( bstr_t )CSValue ) )
{
spFragment = spRoot->createDocumentFragment();
ASSERT( spFragment != NULL );
MSXML::IXMLDOMElementPtr spSubNodes = spNewDoc->documentElement;
ASSERT( spSubNodes != NULL );
while( spSubNodes->hasChildNodes() )
spFragment->appendChild( spSubNodes->firstChild );
TRACE( _T( "%s\n" ), ( LPCTSTR )spFragment->xml );
return( S_OK );
}
return( E_FAIL );
}
And I call it by:
MSXML::IXMLDOMDocumentFragmentPtr spFragment = NULL;
MSXML::IXMLDOMNodePtr spParent = spAttrib->GetparentNode();
MSXML::IXMLDOMDocumentPtr spRoot = spParent->ownerDocument;
ParseSnippetIntoFrag( spRoot, CSValue, spFragment );
MSXML::IXMLDOMNodeListPtr spNodes = spRoot->getElementsByTagName( (
bstr_t )GetName() );
ASSERT( spNodes != NULL );
MSXML::IXMLDOMNodePtr spTargetNode = spNodes->item[ 0 ];
ASSERT( spTargetNode != NULL );
TRACE( _T( "new: %s\n" ), ( LPCTSTR )spFragment->xml ); // looks good
TRACE( _T( "old: %s\n" ), ( LPCTSTR )spTargetNode->xml ); // looks good
spRoot->documentElement->replaceChild( spFragment, spTargetNode );
TRACE( _T( "%s\n" ), ( LPCTSTR )spRoot->xml );
I see the proper new node and target node xml, so it appears the nodes are
correct, but the replaceChild gives me an E_INVALIDARG error.
Did I miss something in how I translated your JScript?
Thanks in Advance!
Corey.
"Martin Honnen" <mahotrash@y...> wrote in message
news:%23yCLWBhLFHA.1392@T......
>
>
> Corey Wirun wrote:
>
>
> > I have a small problem with replacing a node in a loaded document. I've
got
> > a document loaded into an IXMLDOMDocument. It looks something like this:
> >
> > <ELEM1>
> > <CHILD1>test1</CHILD1>
> > <CHILD2><SUB1>subtest</SUB1></CHILD2>
> > </ELEM1>
> >
> > And I have an XML string that looks like this:
> >
> > <CHILD2><SUB1>subtest2</SUB1></CHILD2>
> >
> > I need to remove the document's //ELEM1/CHILD2, and replace it with the
one
> > in the string. I tried loading the string into it's own document,
getting
> > the node ptr and trying replaceChild on the loaded document under
//ELEM1,
> > but it asserts.
>
> With MSXML 3 the following JScript works for me, it creates a new
> document to use its loadXML method to parse and load the string with the
> XML snippet, then it creates a documentFragment node of the original
> document and moves the parsed nodes into the fragment node.
> After that you are free to use the fragment node as needed, for instance
> to replace other contents with the contents of the fragment using normal
> DOM methods like replaceChild:
>
> function parseSnippetIntoFragment (xmlSnippet, ownerDocument) {
> var documentFragment = null;
> var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
> xmlDocument.async = false;
> var loaded = xmlDocument.loadXML('<dummy>' + xmlSnippet + '<\/dummy>');
> if (loaded) {
> documentFragment = ownerDocument.createDocumentFragment();
> var dummy = xmlDocument.documentElement;
> while (dummy.hasChildNodes()) {
> documentFragment.appendChild(dummy.firstChild);
> }
> }
> return documentFragment;
> }
>
>
> // example use
>
> var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
> xmlDocument.async = false;
> var loaded = xmlDocument.load('test2005032001.xml');
>
> var documentFragment =
> parseSnippetIntoFragment('<child><descendant>Kibo<\/descendant><\/child>',
> xmlDocument);
>
> xmlDocument.documentElement.replaceChild(documentFragment,
> xmlDocument.getElementsByTagName('CHILD2').item(0));
>
>
> The same works for me with MSXML 4 (e.g. Msxml2.DOMDocument.4.0).
>
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
