开发者

Add namespaces in IXMLDOMDocument root element using C++

I know there is an example in stackoverflow using JS, but I need to accomplish this using C++ and by a strange reason I am not able to add the namespace to the root element using put_nodeValue() if the attribute name is prefixed with "xmlns:".

So, I need something like this:

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
    <media:group />
    <media:group />
</entry>

I am trying with the following code:

HRESULT hr = S_OK;
IXMLDOMDocument *doc;
IXMLDOMNode *entryElement;
IXMLDOMNode *groupElement;
IXMLDOMNode *groupElement2;
IXMLDOMNode *titleElement;
IXMLDOMNode *mediaAttribute;

// Initialize ...
hr = CoInitialize(NULL);
assert(hr == S_OK);

// Create (root) document
hr = CoCreateInstance(
    CLSID_DOMDocument30,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IXMLDOMDocument,
    (void**)&doc);
assert(hr == S_OK);

// De开发者_开发技巧fine namespaces
BSTR atomNamespace = L"http://www.w3.org/2005/Atom";
BSTR mediaNamespace = L"http://search.yahoo.com/mrss/";

// Define types
VARIANT elementType;
VariantInit(&elementType);
V_VT(&elementType) = VT_INT;
V_INT(&elementType) = NODE_ELEMENT;

VARIANT attributeType;
VariantInit(&attributeType);
V_VT(&attributeType) = VT_INT;
V_INT(&attributeType) = NODE_ATTRIBUTE;

// Add "entry" element
BSTR nodeName = L"entry";
hr = doc->createNode(elementType, nodeName, atomNamespace, &entryElement);
assert(hr == S_OK);

hr = doc->appendChild(entryElement, NULL);
assert(hr == S_OK);

// Add "media" namespace to "entry" element
nodeName = L"xmlns:media";
doc->createNode(attributeType, nodeName, atomNamespace, &mediaAttribute);

IXMLDOMNamedNodeMap *attributes;
entryElement->get_attributes(&attributes);

IXMLDOMNode *newAttribute;
hr = attributes->setNamedItem(mediaAttribute, &newAttribute);
assert(hr == S_OK);

VARIANT nodeValue;
VariantInit(&nodeValue);
V_VT(&nodeValue) = VT_BSTR;
V_BSTR(&nodeValue) = mediaNamespace;
hr = newAttribute->put_nodeValue(nodeValue);
assert(hr == S_OK);

// Add "group" element
nodeName = L"media:group";
hr = doc->createNode(elementType, nodeName, mediaNamespace, &groupElement);
assert(hr == S_OK);

hr = entryElement->appendChild(groupElement, NULL);
assert(hr == S_OK);

// Add second "group" element
nodeName = L"media:group";
hr = doc->createNode(elementType, nodeName, mediaNamespace, &groupElement2);
assert(hr == S_OK);

hr = entryElement->appendChild(groupElement2, NULL);
assert(hr == S_OK);

Any idea? I am using MSXML6.


I got it! I received some help :)

// Create (root) document
hr = CoCreateInstance(
    CLSID_DOMDocument30,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IXMLDOMDocument,
    (void**)&doc);
assert(hr == S_OK);

// Define namespaces
BSTR atomNamespace = L"http://www.w3.org/2005/Atom";
BSTR mediaNamespace = L"http://search.yahoo.com/mrss/";

CComVariant elementType = NODE_ELEMENT;
CComVariant attributeType = NODE_ATTRIBUTE;

// Add "entry" element
hr = doc->createNode(elementType, CComBSTR(L"entry"), atomNamespace, &entryNode);
assert(hr == S_OK);

hr = doc->appendChild(entryNode, NULL);
assert(hr == S_OK);

// Create "xmlns:media" attribute
CComPtr<IXMLDOMElement> element;
CComPtr<IXMLDOMAttribute> attr;

doc->createNode(attributeType, CComBSTR(L"xmlns:media"), mediaNamespace, &attrNode);
assert(hr == S_OK);

hr = attrNode->put_nodeValue(CComVariant(mediaNamespace));
assert(hr == S_OK);

entryNode->QueryInterface(&element);
assert(hr == S_OK);

attrNode->QueryInterface(&attr);
assert(hr == S_OK);

element->setAttributeNode(attr, NULL);

// Add first "group" element
hr = doc->createNode(elementType, CComBSTR(L"media:group"), mediaNamespace, &groupNode1);
assert(hr == S_OK);

hr = entryNode->appendChild(groupNode1, NULL);
assert(hr == S_OK);

// Add second "group" element
hr = doc->createNode(elementType, CComBSTR(L"media:group"), mediaNamespace, &groupNode2);
assert(hr == S_OK);

hr = entryNode->appendChild(groupNode2, NULL);
assert(hr == S_OK);

I just needed to add it as attribute and query the correct interfaces.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜