Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

XML/XSL streamed IHTMLDocument2

Posted on 2003-03-16
6
Medium Priority
?
680 Views
Last Modified: 2013-12-03
I have a web browser control. In my application, I want to generate XML documents (in memory) and inject them into the control using pre defined XSL sheets. I've tried a simple code:

<?xml-stylesheet type="text/xsl" href="d:/test.xsl"?>
<Person>
     <Name>Gilad</Name>
</Person>

I've also put a file called text.xsl on D:\.
I have the following code:

void CMainView::Load(IStream* pStream)
{
     CComPtr<IDispatch> pDispatch;
     pDispatch.Attach(GetHtmlDocument());
     CComQIPtr<IHTMLDocument2> pDocument(pDispatch);
     CComQIPtr<IPersistStreamInit> pPersistInit(pDocument);
     pPersistInit->InitNew();
     pPersistInit->Load(pStream);
}

void CMainView::Load(LPCTSTR szText)
{
     DWORD dwLength=lstrlen(szText);
     HGLOBAL hHandle=GlobalAlloc(GPTR,dwLength);
     CopyMemory(GlobalLock(hHandle),szText,dwLength);
     GlobalUnlock(hHandle);
     CComPtr<IStream> pStream;
     if (SUCCEEDED(CreateStreamOnHGlobal(hHandle,TRUE,&pStream)))
          Load(pStream);
     else
          GlobalFree(hHandle);
}

When I try to load the XML using the functions, I can see the entire code in the control instead the processed output (using the xsl). If I try to navigate the XML file (using Navigate/Navigate2), everything in OK. I DON'T want to use navigate because all the XML creation is made in memory and I don't want to save it to disk. How can I inject it directly into the document?
0
Comment
Question by:gilad_no
  • 4
  • 2
6 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 8147492
I think you are looking for the transformNodeToObject  method of the XMLDOMDocument object.  The output is a stream or an XMLDOMDocument.  I'll see if I can cobble together and example
-- Dan
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 1200 total points
ID: 8147680
First let me say that C++ is absolutely the hardest language to use when working with COM objects.  I started creating an example for you, but I thought of a better way to show the basics.  JScript makes the object creation easy.  So here are three files for you

=-=-=-=-=-=-=-=-=- start of file Data.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Data>
<Person>
    <Name>Gilad</Name>
    <Age>17</Age>
</Person>
<Person>
    <Name>Buford</Name>
    <Age>99</Age>
</Person>
</Data>

=-=-=-=-=-=-=-=-=- end of file

=-=-=-=-=-=-=-=-=- start of file Style.xml
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<COMMENT> using &#160; for hardspace </COMMENT>
<H2><CENTER>Here is the output!<SUP><FONT SIZE="-1">TM</FONT></SUP> </CENTER></H2>

<TABLE border="2">
<xsl:for-each select="/Data/Person">
<TR><TD ALIGN='right'>Name:</TD>    <TD><xsl:value-of select="Name"/> </TD></TR>
<TR><TD ALIGN='right'>Age:</TD>     <TD><xsl:value-of select="Age"/>  </TD></TR>
</xsl:for-each>
</TABLE>
<BR/>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
=-=-=-=-=-=-=-=-=- end of file

=-=-=-=-=-=-=-=-=- start of file Tester.HTA
<html>
<body>
<script>
function DoClick() {
     // load data
     alert("starting");
     var oXml= new ActiveXObject("Microsoft.XMLDOM");
     oXml.async = false;
     oXml.load("c:\\temp\\data.xml");

     // load style sheet
     var oSheet= new ActiveXObject("Microsoft.XMLDOM");
     oSheet.async= false;
     oSheet.load("c:\\temp\\style.xsl");

     // set up the resulting document
     var oOutput= new ActiveXObject("Microsoft.XMLDOM");
     oOutput.async= false;
     oOutput.validateOnParse= true;

     // parse results into a result DOM Document
     oXml.transformNodeToObject( oSheet, oOutput);  

     //var s= oOutput.nodeValue;
     //var s= oOutput;
     var s= oOutput.documentElement.xml;
     alert(s);
}

</script>
<input type=button value="Click me" onclick="DoClick();">
<input type=button value="Reload" onclick="document.location.reload();">

</body>
</html>
=-=-=-=-=-=-=-=-=- end of file

Create the three files adn then double-click the HTA file.
The procedure is to create three XMLDOMDocument objects.  Load one with the data and one with the sytle sheet.  Then use transformNode to apply the stylesheet to the XML and output into the third.  As an example, I'm just spewing the resulting XML as text into an alert box, but you could insert it into a DIV or stream it itno a new HTML Document or whatever.

If you need doing either of these latter options, or if you need help translating the JScript into C++, please ask  -- I'd like a chance to earn an A on this Question. Thanks.

-- Dan
0
 

Author Comment

by:gilad_no
ID: 8150305
I've tried to use transformNode instead, but it doesn't work.

IXMLDOMDocumentPtr pDocument,pSheet;

// szText contains the xml data
pDocument.CreateInstance(CLSID_DOMDocument);
pDocument->put_async(VARIANT_FALSE);
pDocument->loadXML((_bstr_t)(LPCTSTR)szText);

// szSheet contains the xsl sheet
pSheet.CreateInstance(CLSID_FreeThreadedDOMDocument);
pSheet->put_async(VARIANT_FALSE);
pSheet->loadXML((_bstr_t)(LPCTSTR)szSheet);

IXMLDOMNodePtr pRoot=pDocument->GetdocumentElement();
IXMLDOMNodePtr pRootSheet=pSheet->GetdocumentElement();

m_view.Load((LPCTSTR)(_bstr_t)pRoot->transformNode(pRootSheet));

In the last statement, I always recieve an empty string.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

Author Comment

by:gilad_no
ID: 8150463
It works!

pDocument.CreateInstance(CLSID_DOMDocument);
pDocument->put_async(VARIANT_FALSE);
pDocument->loadXML((_bstr_t)(LPCTSTR)szText);

pSheet.CreateInstance(CLSID_FreeThreadedDOMDocument);
pSheet->put_async(VARIANT_FALSE);
pSheet->loadXML((_bstr_t)(LPCTSTR)szSheet);

CString szOutput((LPCTSTR)(_bstr_t)pDocument->transformNode(pSheet));
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8150469
Greate! You are almost there!   Please look a little more closely at my post.  The final few lines create a THIRD xml document object and use it in a call to the
    transformNodeToObject
member function.  I'm confident that if you take that last simple step, you will be rewarded with a great measure of satisfaction!  Happiness is within your grasp!

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8150484
Joy ripples though my entire being at your success.
-- Dan
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Integration Management Part 2

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question