?
Solved

XML/XSL streamed IHTMLDocument2

Posted on 2003-03-16
6
Medium Priority
?
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 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