EDN Admin
Well-known member
hi,all, I want to read in a string of snippet of html source and get all the styles of html elements within it, and I use hash_map to store the values; this is the code:
<div style="color:Black;background-color:White; <pre>
wstring wstrTxt2 = L<span style="color:#A31515; "<A href=/servlet/BookDetailsPL?bi=1257056972&tab=1&searchurl=bt.x%3D44%26bt.y%3D10%26sts%3Dt%26tn%3Dharry%2Bpotter cmImpressionSent=1>The Orchard Bookshop.</A> <SPAN class=scndInfo>(Hayes., UK, United Kingdom)</SPAN>";
hash_map<wstring, wstring> hmStyles = GetNodeStyles(_variant_t(wstrTxt2.c_str()));
hash_map<wstring, wstring> CXMLDOMFromVCDlg::GetNodeStyles(VARIANT varSrc)
{
<span style="color:Blue; long lLength = 0;
MSHTML::IHTMLDocument2Ptr htmDoc = NULL;
MSHTML::IHTMLElementCollectionPtr pElemColl = NULL;
MSHTML::IHTMLElementPtr pChElem = NULL;
MSHTML::IHTMLStylePtr pStyle = NULL;
BSTR bstrFtStyle;
hash_map<wstring, wstring> hmStyles;
SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (<span style="color:Blue; void**) &htmDoc);
VARIANT *param;
HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = varSrc.bstrVal;
hr = SafeArrayUnaccessData(psaStrings);
hr = htmDoc->write(psaStrings);
pElemColl = htmDoc->all;
lLength = pElemColl->length;
<span style="color:Blue; for(<span style="color:Blue; long i = 0; i < lLength; i++)
{
pChElem = pElemColl->item(_variant_t(i), _variant_t());
pStyle = pChElem->style;
pStyle->get_fontStyle(&bstrFtStyle);
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->fontStyle)) != hmStyles.end()) <span style="color:Green; //this is where the code crashed
hmStyles[wstring(pStyle->fontStyle)] = L<span style="color:#A31515; "font style";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->fontFamily)) != hmStyles.end())
hmStyles[wstring(pStyle->fontFamily)] = L<span style="color:#A31515; "font family";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->fontWeight)) != hmStyles.end())
hmStyles[wstring(pStyle->fontWeight)] = L<span style="color:#A31515; "font weight";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->textDecoration)) != hmStyles.end())
hmStyles[wstring(pStyle->textDecoration)] = L<span style="color:#A31515; "text decoration";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->color.bstrVal)) != hmStyles.end())
hmStyles[wstring(pStyle->color.bstrVal)] = L<span style="color:#A31515; "font color";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->fontSize.bstrVal)) != hmStyles.end())
hmStyles[wstring(pStyle->fontSize.bstrVal)] = L<span style="color:#A31515; "font size";
}
<span style="color:Blue; return hmStyles;
}
[/code]
i make the style values as keys, and style names as strings, e.g. "italic fontstyle";
the runtime error is "invalid null pointer".
so what went wrong?
cheers
daiyue
View the full article
<div style="color:Black;background-color:White; <pre>
wstring wstrTxt2 = L<span style="color:#A31515; "<A href=/servlet/BookDetailsPL?bi=1257056972&tab=1&searchurl=bt.x%3D44%26bt.y%3D10%26sts%3Dt%26tn%3Dharry%2Bpotter cmImpressionSent=1>The Orchard Bookshop.</A> <SPAN class=scndInfo>(Hayes., UK, United Kingdom)</SPAN>";
hash_map<wstring, wstring> hmStyles = GetNodeStyles(_variant_t(wstrTxt2.c_str()));
hash_map<wstring, wstring> CXMLDOMFromVCDlg::GetNodeStyles(VARIANT varSrc)
{
<span style="color:Blue; long lLength = 0;
MSHTML::IHTMLDocument2Ptr htmDoc = NULL;
MSHTML::IHTMLElementCollectionPtr pElemColl = NULL;
MSHTML::IHTMLElementPtr pChElem = NULL;
MSHTML::IHTMLStylePtr pStyle = NULL;
BSTR bstrFtStyle;
hash_map<wstring, wstring> hmStyles;
SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (<span style="color:Blue; void**) &htmDoc);
VARIANT *param;
HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = varSrc.bstrVal;
hr = SafeArrayUnaccessData(psaStrings);
hr = htmDoc->write(psaStrings);
pElemColl = htmDoc->all;
lLength = pElemColl->length;
<span style="color:Blue; for(<span style="color:Blue; long i = 0; i < lLength; i++)
{
pChElem = pElemColl->item(_variant_t(i), _variant_t());
pStyle = pChElem->style;
pStyle->get_fontStyle(&bstrFtStyle);
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->fontStyle)) != hmStyles.end()) <span style="color:Green; //this is where the code crashed
hmStyles[wstring(pStyle->fontStyle)] = L<span style="color:#A31515; "font style";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->fontFamily)) != hmStyles.end())
hmStyles[wstring(pStyle->fontFamily)] = L<span style="color:#A31515; "font family";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->fontWeight)) != hmStyles.end())
hmStyles[wstring(pStyle->fontWeight)] = L<span style="color:#A31515; "font weight";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->textDecoration)) != hmStyles.end())
hmStyles[wstring(pStyle->textDecoration)] = L<span style="color:#A31515; "text decoration";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->color.bstrVal)) != hmStyles.end())
hmStyles[wstring(pStyle->color.bstrVal)] = L<span style="color:#A31515; "font color";
<span style="color:Blue; if(hmStyles.find(wstring(pStyle->fontSize.bstrVal)) != hmStyles.end())
hmStyles[wstring(pStyle->fontSize.bstrVal)] = L<span style="color:#A31515; "font size";
}
<span style="color:Blue; return hmStyles;
}
[/code]
i make the style values as keys, and style names as strings, e.g. "italic fontstyle";
the runtime error is "invalid null pointer".
so what went wrong?
cheers
daiyue
View the full article