2011-08-09 83 views
0

我知道使用JS的there is an example in stackoverflow,但我需要使用C++完成此操作,並且由於一個奇怪的原因,我無法使用put_nodeValue()將名稱空間添加到根元素,如果屬性名稱前綴爲「xmlns:」。使用C++在IXMLDOMDocument根元素中添加命名空間

所以,我需要這樣的:

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

我用下面的代碼嘗試:

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); 

// Define 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); 

任何想法?我正在使用MSXML6。

回答

0

我明白了!我收到了一些幫助:)

// 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); 

我只是需要添加它作爲屬性和查詢正確的接口。

相關問題