在VC中使用MSXML创建SVG,在使用下面代码:
//创建一个层 pLeyer = pDoc->createElement((_bstr_t)"g"); pLeyer->setAttribute("id","Head_Layer"); //创建一个节点 pNode = pDoc->createElement((_bstr_t)"rect"); pNode->setAttribute("x","0"); pNode->setAttribute("y","0"); pNode->setAttribute("width","1650"); pNode->setAttribute("height","906"); pNode->setAttribute("fill","rgb(0,0,0)"); //添加节点到层 pLeyer->appendChild(pNode); //将层添加到根 xmlRoot->appendChild(pLeyer);
建立一个新结点时,生成的结点中出现xmlns="" 属性,如下xml:
<g xmlns="" id="Head_Layer"> <rect x="0" y="0" width="1650" height="906" fill="rgb(0,0,0)"/> </g>
自动的添加了xmlns="" 属性,查了资料,解决办法如下代码:
/********引用包含**********/ VARIANT vtTemp; vtTemp.vt = VT_I2; vtTemp.iVal = 1; _bstr_t namespaceURI="http://www.w3.org/2000/svg"; //创建一个层 pLeyer = pDoc->createNode(vtTemp,(_bstr_t)"g",namespaceURI); pLeyer->setAttribute("id","Head_Layer"); //创建一个节点 pNode = pDoc->createNode(vtTemp,(_bstr_t)"rect",namespaceURI); pNode->setAttribute("x","0"); pNode->setAttribute("y","0"); pNode->setAttribute("width","1650"); pNode->setAttribute("height","906"); pNode->setAttribute("fill","rgb(0,0,0)"); //添加节点到层 pLeyer->appendChild(pNode); //将层添加到根 xmlRoot->appendChild(pLeyer);
使用createNode创建结点。