You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by Milan Tomic <mi...@setcce.org> on 2003/12/01 17:28:33 UTC

DOMNode to char*

Berin,

    I have one (beginner) question. It is more about Xerces. I can't get
DOMNode (and all its children nodes) as char*. In templatesign.exe there
is a way to send DOMNode to standard output:

DOMNode *doc;
...
cout << doc;

    But, I dont wont to put doc in stdout, I'd like to put it into char*
variable. Do you know how? Example:

char* result = doc->GetXML();// there is no GetXML() function

    I wrote this:

DOMWriter *dw = impl->createDOMWriter();
XMLCh* xcXML = dw->writeToString(*doc);
char *cXML = XMLString::transcode(xcXML);
delete dw;

    But, when save result (cXML) in file I got an error because this is
UTF-16 encoding and I need UTF-8.

Thank you.

RE: DOMNode to char*

Posted by Milan Tomic <mi...@setcce.org>.
> Have you tried the DOMDocument::setEncoding(XMLCh *) method?
No, it doesn't work.  :(  Still, there is "UTF-16" string in the output.

I'm creating DOMDocument like this:

DOMDocument *theDOM;
const XMLCh* encNameStr = XMLString::transcode("UTF-8");

XMLCh tempStr[100];
XMLString::transcode("Core", tempStr, 99);    
DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(tempStr);
theDOM = impl->createDocument(
	0,                    // root element namespace URI.
	MAKE_UNICODE_STRING("ROOT"),            // root element name
	NULL);// DOMDocumentType());  // document type object (DTD).
theDOM->setEncoding(encNameStr);

When using it like this:

//cout << rootNode;
std::ostringstream oss;
oss << rootNode;

it works fine (there is "UTF-8" instead of "UTF-16"), but it took about
2 minutes to compleate. I wonder where this repleacment happens?

Thank you very much,
Milan



Re: DOMNode to char*

Posted by Berin Lautenbach <be...@ozemail.com.au>.
Hmm.  Unexpected.

Have you tried the DOMDocument::setEncoding(XMLCh *) method?

Cheers,
	Berin

Milan Tomic wrote:
> Berin,
> 
> 
>>DOMWriter *dw = impl->createDOMWriter();
>>encNameStr = XMLString::transcode("UTF-8");
>>dw->setEncoding(encNameString);
>>XMLCh* xcXML = dw->writeToString(*doc);
>>char *cXML = XMLString::transcode(xcXML);
>>delete dw;
>>
>>Should work.
> 
>     I've just tried that and still there is:
> 
> <?xml version="1.0" encoding="UTF-16" standalone="no" ?>
> 
>     line in my output XML document. "UTF-16" should be "UTF-8". The file
> was already encoded right even without this line:
> 
> 
>>dw->setEncoding(encNameString);
> 
> 
>     the only problem is first line in my output document which contains
> "UTF-16" string. When I change it in notepad ("UTF-16" to "UTF-8")
> everything works fine.
> 
> Thank you,
> Milan
> 
> 
> 


RE: DOMNode to char*

Posted by Milan Tomic <mi...@setcce.org>.
Berin,

> DOMWriter *dw = impl->createDOMWriter();
> encNameStr = XMLString::transcode("UTF-8");
> dw->setEncoding(encNameString);
> XMLCh* xcXML = dw->writeToString(*doc);
> char *cXML = XMLString::transcode(xcXML);
> delete dw;
> 
> Should work.
    I've just tried that and still there is:

<?xml version="1.0" encoding="UTF-16" standalone="no" ?>

    line in my output XML document. "UTF-16" should be "UTF-8". The file
was already encoded right even without this line:

> dw->setEncoding(encNameString);

    the only problem is first line in my output document which contains
"UTF-16" string. When I change it in notepad ("UTF-16" to "UTF-8")
everything works fine.

Thank you,
Milan



Re: DOMNode to char*

Posted by Berin Lautenbach <be...@ozemail.com.au>.
Milan,

The DOMWriter can actually be told directly to output UTF-16 (or any 
other known encoding) :

DOMWriter *dw = impl->createDOMWriter();
encNameStr = XMLString::transcode("UTF-8");
dw->setEncoding(encNameString);
XMLCh* xcXML = dw->writeToString(*doc);
char *cXML = XMLString::transcode(xcXML);
delete dw;

Should work.

Cheers,
	Berin

Milan Tomic wrote:
>  
>     That is exactly what I did. The only problem is that it took me 
> about 2 minutes on my PC (P4 2.6 GHz, 512 MB RAM) to write 1,5 MB in 
> ostringstream. So, I've done this:
>  
> DOMWriter *dw = impl->createDOMWriter();
> XMLCh* xcXML = dw->writeToString(*doc);
> char *cXML = XMLString::transcode(xcXML);
> delete dw;
>  
>     It works fine, but there is "UTF-16" specifier in the first line of 
> my XML document:
>  
> <?xml version="1.0" encoding="UTF-16" standalone="no" ?>
>  
>     I've tried this (from templatesign.cpp), to replace "UTF-16" with 
> "UTF-8":
>  
> encNameStr = XMLString::transcode("UTF-8");
> aNode = doc->getFirstChild();
> if (aNode->getNodeType() == DOMNode::ENTITY_NODE)
> {
>     ((DOMEntity *)aNode)->setEncoding(encNameStr);// never comes here.
> }
>  
>     But, it doesn't work. It never steps into "if".
>  
> Thank you.
>  
>  
>  
> 
>     -----Original Message-----
>     *From:* Scott Cantor [mailto:cantor.2@osu.edu]
>     *Sent:* Monday, December 01, 2003 5:31 PM
>     *To:* security-dev@xml.apache.org
>     *Subject:* RE: DOMNode to char*
> 
>     Just write a link between the serializer and an ostream&. Then you
>     can dump to cout or to a ostrstream or ostringstream and extract a
>     char* from that.
>      
>     -- Scott
>      
>     -----Original Message-----
>     *From:* Milan Tomic [mailto:milan@setcce.org]
>     *Sent:* Monday, December 01, 2003 11:29 AM
>     *To:* security-dev@xml.apache.org
>     *Subject:* DOMNode to char*
> 
>         Berin,
> 
>             I have one (beginner) question. It is more about Xerces. I
>         can't get DOMNode (and all its children nodes) as char*. In
>         templatesign.exe there is a way to send DOMNode to standard output:
> 
>         DOMNode *doc;
>         ...
>         cout << doc;
> 
>             But, I dont wont to put doc in stdout, I'd like to put it
>         into char* variable. Do you know how? Example:
> 
>         char* result = doc->GetXML();// there is no GetXML() function
> 
>             I wrote this:
> 
>         DOMWriter *dw = impl->createDOMWriter();
>         XMLCh* xcXML = dw->writeToString(*doc);
>         char *cXML = XMLString::transcode(xcXML);
>         delete dw;
> 
>             But, when save result (cXML) in file I got an error because
>         this is UTF-16 encoding and I need UTF-8.
> 
>         Thank you.
> 


RE: DOMNode to char*

Posted by Milan Tomic <mi...@setcce.org>.
 
    That is exactly what I did. The only problem is that it took me
about 2 minutes on my PC (P4 2.6 GHz, 512 MB RAM) to write 1,5 MB in
ostringstream. So, I've done this:
 
DOMWriter *dw = impl->createDOMWriter();
XMLCh* xcXML = dw->writeToString(*doc);
char *cXML = XMLString::transcode(xcXML);
delete dw;
 
    It works fine, but there is "UTF-16" specifier in the first line of
my XML document:
 
<?xml version="1.0" encoding="UTF-16" standalone="no" ?>
 
    I've tried this (from templatesign.cpp), to replace "UTF-16" with
"UTF-8":
 
encNameStr = XMLString::transcode("UTF-8");
aNode = doc->getFirstChild();
if (aNode->getNodeType() == DOMNode::ENTITY_NODE)
{
    ((DOMEntity *)aNode)->setEncoding(encNameStr);// never comes here.
}
 
    But, it doesn't work. It never steps into "if".
 
Thank you.
 
 
 

-----Original Message-----
From: Scott Cantor [mailto:cantor.2@osu.edu] 
Sent: Monday, December 01, 2003 5:31 PM
To: security-dev@xml.apache.org
Subject: RE: DOMNode to char*


Just write a link between the serializer and an ostream&. Then you can
dump to cout or to a ostrstream or ostringstream and extract a char*
from that.
 
-- Scott
 
-----Original Message-----
From: Milan Tomic [mailto:milan@setcce.org] 
Sent: Monday, December 01, 2003 11:29 AM
To: security-dev@xml.apache.org
Subject: DOMNode to char*



Berin, 

    I have one (beginner) question. It is more about Xerces. I can't get
DOMNode (and all its children nodes) as char*. In templatesign.exe there
is a way to send DOMNode to standard output:

DOMNode *doc; 
... 
cout << doc; 

    But, I dont wont to put doc in stdout, I'd like to put it into char*
variable. Do you know how? Example: 

char* result = doc->GetXML();// there is no GetXML() function 

    I wrote this: 

DOMWriter *dw = impl->createDOMWriter(); 
XMLCh* xcXML = dw->writeToString(*doc); 
char *cXML = XMLString::transcode(xcXML); 
delete dw; 

    But, when save result (cXML) in file I got an error because this is
UTF-16 encoding and I need UTF-8. 

Thank you. 


RE: DOMNode to char*

Posted by Scott Cantor <ca...@osu.edu>.
Just write a link between the serializer and an ostream&. Then you can dump
to cout or to a ostrstream or ostringstream and extract a char* from that.
 
-- Scott
 
-----Original Message-----
From: Milan Tomic [mailto:milan@setcce.org] 
Sent: Monday, December 01, 2003 11:29 AM
To: security-dev@xml.apache.org
Subject: DOMNode to char*



Berin, 

    I have one (beginner) question. It is more about Xerces. I can't get
DOMNode (and all its children nodes) as char*. In templatesign.exe there is
a way to send DOMNode to standard output:

DOMNode *doc; 
... 
cout << doc; 

    But, I dont wont to put doc in stdout, I'd like to put it into char*
variable. Do you know how? Example: 

char* result = doc->GetXML();// there is no GetXML() function 

    I wrote this: 

DOMWriter *dw = impl->createDOMWriter(); 
XMLCh* xcXML = dw->writeToString(*doc); 
char *cXML = XMLString::transcode(xcXML); 
delete dw; 

    But, when save result (cXML) in file I got an error because this is
UTF-16 encoding and I need UTF-8. 

Thank you.