我知道there is an example in stackoverflow使用JS,但是我需要使用C ++实现这一点,并且由于一个奇怪的原因,如果属性名称以“为前缀”,我无法使用put_nodeValue()将命名空间添加到根元素XMLNS:”
所以,我需要这样的东西:
<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 :(得分: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);
我只需将其添加为属性并查询正确的接口。