views:

75

answers:

1

I need to add "annotations" to existing HTML documents - best in the form of string property values I can read & write by name.

Apparently (to me), meta elements in the header seem to be the common way - i.e. adding/modifying elements like

<head>
  <meta name="unique-id_property-name"  content="property-value"/>
  ...
</head>

Question 1: Ist that "acceptable" / ok, or is there a better way to add meta data?

I have a little previous experience with getting/mut(il)ating HTML contents through the document in an web browser control. For this task, I've already loaded the HTML document into a HTMLDocument object, but I'm not sure how to go on:

// what I have:

IHTMLDocument2Ptr doc;
doc.CreateInstance(__uuidof(HTMLDocument));
IPersistFile pf = doc;
pf->Load(fileName, STGM_READ);  

// ... what now?

Questions 2: Should I be using anything else than HTMLDocument?

Questions 3..N: How do I get the head element? How do I get the value of a meta element with a given name? How do I set the value of a meta element (adding the item if and only if it doesn't exist yet)?

doc->all returns a collection of all tags, which I can enumerate even though count returns 0. I could scan that for head, then scan that for all meta where the name starts with a certain string, etc. - but this feels very clumsy.

+1  A: 

I do not have enough experience working with HTML using C++. I am not sure if this answers your question, but you could do the following

  1. enum all elements using doc->all
  2. for each IHtmlElement you can compare the tagname to meta (use IHtmlElement::get_tagName)
  3. for these meta tags you can get and set property using IHtmlElement::getAttribute and setAttribute methods)

Or

  1. Write a Javascript function that accepts a property name and value. This should be able to update the meta tag you have
  2. You can then get the script object using IHtmlDocument2::get_script as IDispatch
  3. Use this script dispatch to Invoke your javascript function.
byte