我有一个操作 xml 的 c++ 应用程序。好吧,在我的应用程序的某个时刻,我得到了一个 DOMNode*,然后我将它作为一个子元素附加到一个元素上。
那么问题是我想向那个节点添加参数......嗯,它是一个节点,所以它不是一个元素......只有元素有参数......
这是我的代码:
xercesc::DOMNode* node = 0;
std::string xml = from_an_obj_of_mine.GetXml(); /* A string with xml inside, the xml is sure an element having something inside */
xercesc::MemBufInputSource xml_buf((const XMLByte*)xml.c_str(), xml.size(), "dummy");
xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser();
parser->parse(xml_buf); /* parser will contain a DOMDocument well parsed from the string, I get here the node i want to attach */
node = my_pointer_to_a_preexisting_domdocument->GetXmlDocument()->importNode(parser->getDocument()->getDocumentElement(), true); /* I want to attach the node in parser to a node of my_pointer_to_an_el_of_my_preexisting_domdocument, it is a different tree, so I must import the node to attach it later */
my_pointer_to_an_el_of_my_preexisting_domdocument->appendChild(node);
如您所见,我想从字符串创建一个节点,我通过解析创建它,然后需要导入该节点以创建一个新的相同节点,该节点属于我要附加新节点的 dom 树。我的步骤是:
获取要附加到预先存在的 dom 的 xml 字符串(作为 domdocument 存储在某处)
创建解析器
使用解析器从字符串创建一个 dom 树
从我预先存在的 dom(我想附加我的新节点)中,调用导入并克隆节点,以便它可以附加到预先存在的 dom。
附上它
问题是导入和导入给我一个节点......我想要一个元素附加......
我也使用 appendChild 来附加元素...当然该方法需要 DOMNode* 但给它一个 DOMElement* (从 DOMNode 继承)是可以的...
如何从节点中获取元素???删除 wd_parser;