Hello,
I'm working to develop a XPCOM component in C++, but I'm having a
bit of difficulty with pointer management.
I am trying to parse a (char *) using the nsIDOMSerializer interface
and return a pointer to a nsIDOMNode * ( that is, the function
signature is int node_return(char *, nsIDOMNode *) ). I am passing an
XML char* string to the function, and parsing the string successfully,
but I'm not able to use the nsIDOMNode* once I'm finished. I am
calling the function using node_return(xml_in,
getter_AddRefs(out_ptr)), but I get a NS_ERROR_ILLEGAL_VALUE when I try
to serialize the returned out_ptr. Can you help?
The program listing is below, followed by sample output. Thanks in
advance for any help you can provide!!!
Patrick
[EMAIL PROTECTED]
-----BEGIN PROGRAM LISTING-----
#include <stdio.h>
#include "nsCOMPtr.h"
#include "nsError.h"
#include "nsIServiceManager.h"
#include "nsISupports.h"
#include "nsIDOMDocument.h"
#include "nsIDOMElement.h"
#include "nsIDOMNode.h"
#include "nsXPCOM.h"
#include "xmlextras/nsIDOMParser.h"
#include "xmlextras/nsIDOMSerializer.h"
#include "string/nsString.h"
nsCOMPtr<nsIDOMParser> parser;
nsCOMPtr<nsIDOMSerializer> serializer;
/* two functions to return a parsed document root element using
an nsIDOMNode interface. */
int node_return(char *, nsIDOMNode **);
int node_return_two(char *, nsIDOMNode **);
int main(int argc, char **argv){
nsresult rv;
nsIServiceManager *servMgr;
int ret = 0;
char *in_str = "<topNode>next node<secondLevelNode>this is
another</secondLevelNode><thirdNode>another
node</thirdNode></topNode>";
/* initialize the XPCOM interface. */
rv = NS_InitXPCOM2(&servMgr, nsnull, nsnull);
if (NS_FAILED(rv)) return rv;
/* create the DOM parser */
parser = do_CreateInstance(NS_DOMPARSER_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
printf("parser creation error - error %02x!\n", rv);
return rv;
}
/* create the DOM serializer */
serializer = do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
printf("serializer creation error - %02x!\n", rv);
return rv;
}
/* out_node is the nsCOMPtr for the result. */
nsCOMPtr<nsIDOMNode> out_node; nsAutoString out_str;
node_return(in_str, getter_AddRefs(out_node));
rv = serializer->SerializeToString( out_node, out_str );
if ( NS_FAILED(rv) ) {
printf("serialization failed - error code %02x.\n", rv);
}
printf("second serializer output is\n%s\n",
NS_ConvertUTF16toUTF8(out_str).get());
/* out_node_two is the nsIDOMNode pointer that is used to transfer
the result */
nsIDOMNode *out_node_two; nsAutoString out_str_two;
node_return_two(in_str, &out_node_two);
rv = serializer->SerializeToString( out_node_two, out_str_two );
if ( NS_FAILED(rv) ) {
printf("serialization failed - error code %02x.\n", rv);
return -1;
/* return NS_ERROR_FAILURE; */
}
printf("second serializer output is\n%s\n",
NS_ConvertUTF16toUTF8(out_str_two).get());
return ret;
}
int node_return(char *in_xml_string, nsIDOMNode **node_ptr){
nsresult rv;
nsCOMPtr<nsIDOMDocument> doc_one;
/* parse the input xml document */
rv = parser->ParseFromString( (const PRUnichar *)
NS_ConvertUTF8toUTF16(in_xml_string).get(),
"text/xml", getter_AddRefs(doc_one) );
if (NS_FAILED(rv)) {
printf("document parsing failed - error code is %02x!\n", rv);
return rv;
}
/* get the root element */
nsCOMPtr<nsIDOMElement> root_elem;
doc_one->GetDocumentElement(getter_AddRefs(root_elem));
/* create a nsIDOMNode nsCOMPtr and assign the root element
nsIDOMElement nsCOMPtr
to the new nsCOMPtr<nsIDOMNode>. Then swap that
nsCOMPtr<nsIDOMNode> with the input pointer. */
nsCOMPtr<nsIDOMNode> element_three;
element_three = (nsIDOMNode *) root_elem;
element_three.swap(*node_ptr);
nsAutoString output_string;
serializer->SerializeToString(root_elem, output_string);
if ( NS_FAILED(rv) ) {
printf("1 - failed - error code %02x.\n", rv);
return -1;
/* return NS_ERROR_FAILURE; */
}
printf("serializer output is\n%s\n",
NS_ConvertUTF16toUTF8(output_string).get());
return 0;
}
int node_return_two(char *in_xml_string, nsIDOMNode **node_ptr){
nsresult rv;
nsCOMPtr<nsIDOMDocument> doc_one;
rv = parser->ParseFromString( (const PRUnichar *)
NS_ConvertUTF8toUTF16(in_xml_string).get(),
"text/xml", getter_AddRefs(doc_one) );
if (NS_FAILED(rv)) {
printf("document parsing failed - error code is %02x!\n", rv);
return rv;
}
nsCOMPtr<nsIDOMElement> root_elem;
doc_one->GetDocumentElement(getter_AddRefs(root_elem));
/* do a simple cast to the output element */
*node_ptr = (nsIDOMNode *) root_elem;
nsAutoString output_string;
serializer->SerializeToString(root_elem, output_string);
if ( NS_FAILED(rv) ) {
printf("1 - failed - error code %02x.\n", rv);
return -1;
/* return NS_ERROR_FAILURE; */
}
printf("serializer output is\n%s\n",
NS_ConvertUTF16toUTF8(output_string).get());
return 0;
}
-----END PROGRAM LISTING-----
-----START PROGRAM OUTPUT-----
[EMAIL PROTECTED] icsi-test]$ make npt
c++ -I /usr/include/c++/4.0.0 -g -D__XMLSEC_FUNCTION__=__FUNCTION__
-DXMLSEC_NO_XKMS=1 -I/usr/local/include/xmlsec1 -I/usr/include/libxml2
-DXMLSEC_CRYPTO_DYNAMIC_LOADING=1 -DXMLSEC_CRYPTO=\"openssl\"
-DUNIX_SOCKETS -I /usr/include/mozilla-1.7.12 -I
/usr/include/mozilla-1.7.12/nspr -I /usr/include/mozilla-1.7.12/xpcom
-g -L/usr/local/lib -L/usr/lib -ldl -lxmlsec1 -lxslt -lxml2 -lz
-lpthread -lm -L /o/opt/src/gecko-sdk/lib -L /usr/lib/mozilla-1.7.12 -L
/usr/lib/mozilla-1.7.12/components -lnspr4 -lplds4 -lxpcom
-lxpcom_compat -lxpcomglue -lxmlextras -lgklayout nsPointerTest.cpp
-o nsPointerTest
./nsPointerTest
serializer output is
<topNode>next node<secondLevelNode>this is
another</secondLevelNode><thirdNode>another node</thirdNode></topNode>
serialization failed - error code 80070057.
second serializer output is
serializer output is
<topNode>next node<secondLevelNode>this is
another</secondLevelNode><thirdNode>another node</thirdNode></topNode>
make: *** [npt] Segmentation fault
[EMAIL PROTECTED] icsi-test]$
-----END PROGRAM OUTPUT-----
_______________________________________________
Mozilla-xpcom mailing list
[email protected]
http://mail.mozilla.org/listinfo/mozilla-xpcom