Kirix Support Forums

Get DOM variable "innerHTML"

Please post all general questions, comments, bug reports, and any other wxWebConnect feedback here.

Get DOM variable "innerHTML"

Postby taaraka on Sat Jan 23, 2010 10:01 am

Hi all,

At first thank you Kirix for this great library! :D

I was trying to get document.documentElement.innerHTML as a wxString, but had no success.
Code: Select all
wxDOMElement elem = m_browser->GetDOMDocument().GetDocumentElement()
wxMessageBox(elem.GetNodeName()); // shows "HTML" which is the name of document.documentElement

I tried using GetAttribute(), GetAttributeNode(), ect.
Could someone please tell me how to access the innerHTML object?

Thank you in advance,
taaraka
Registered User
 
Posts: 3
Joined: Tue Jan 19, 2010 1:21 pm

Re: Get DOM variable "innerHTML"

Postby taaraka on Wed Feb 17, 2010 11:10 am

Hello,
I found a workaround: :D

Code: Select all
...
#define TEMP_PATH wxT("/home/usr/webtest.temp")
...
void saveInnerHTML() {
    wxString ihtml = innerHTML(&m_browser->GetDOMDocument().GetLastChild());
    wxTextFile tfile;
    if(!wxFile::Exists(TEMP_PATH))
        tfile.Create(TEMP_PATH);
    else {
        tfile.Open(TEMP_PATH);
        tfile.Clear();
    }
    tfile.AddLine(ihtml);
    tfile.Write();
}
wxString innerHTML(wxDOMNode* node, unsigned depth = 0) {
    wxString data;
    if(node->GetNodeName()!=wxT("#comment"))
    {
        if(node->GetNodeName()==wxT("#text"))
            data = wxString::Format(wxT("%s"), node->GetNodeValue().c_str());
        else {
            data = wxString::Format(wxT("<%s"), node->GetNodeName().Lower().c_str());
            wxDOMNamedNodeMap map = node->GetAttributes();

            for(unsigned u = 0 ; u < map.GetLength() ; u++) {
                data += wxString::Format(wxT(" %s=\"%s\""), map.Item(u).GetNodeName(), map.Item(u).GetNodeValue());
            }
            data += wxString::Format(wxT(">%s"), node->GetNodeValue().c_str());
        }
        wxDOMNodeList lst = node->GetChildNodes();
        for(unsigned u = 0 ; u < lst.GetLength() ; u++) {
            wxDOMElement elem = lst.Item(u);
            data += innerHTML(&lst.Item(u), depth+1);
        }
        if(node->GetNodeName()!=wxT("#text"))
            data += wxString::Format(wxT("</%s>\n"), node->GetNodeName().Lower().c_str());
    }
    return data;
}
...


Would be much easier if I could access innerHTML, but still great library! Thank you,
taaraka
Registered User
 
Posts: 3
Joined: Tue Jan 19, 2010 1:21 pm

Return to wxWebConnect Questions, Thoughts & Feedback