
正文
VC,VB操作XML
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
TCHAR buffer[MAX_PATH] = {};
::GetModuleFileName(NULL, buffer, MAX_PATH);
CString strPath = buffer;
int nIndex = strPath.ReverseFind(_T('\\'));
strPath = strPath.Left(nIndex + );
CString strBomFile = strPath + _T("Bom.xml");
MSXML2::IXMLDOMDocumentPtr pDocXML = NULL;
MSXML2::IXMLDOMElementPtr pRoot = NULL;
MSXML2::IXMLDOMElementPtr pNode = NULL;
HRESULT hr = pDocXML.CreateInstance(__uuidof(MSXML2::DOMDocument));
ASSERT(SUCCEEDED(hr));
pRoot = pDocXML->createElement((_bstr_t)(_T("BomData")));
pDocXML->appendChild(pRoot);
//3-1、遍历BomRcdArr
for(long i = ;i<;i++)
{
CString strFilePath = strBomFile;
//write to xml
short iNum=;
iNum=;
BSTR strKey;
BSTR strValue;
short ii=;
for (ii=;ii<iNum;ii++)
{
strKey = L"KEY";
strValue =L"VALUE";
//pITHDbBomRecorder->GetItem(&strKey,&strValue,ii);
pNode=NULL;
pNode = pDocXML->createElement((_bstr_t)(_T("Node")));
pNode->put_text((_bstr_t)_T("BomData"));//设置标签的文本内容;
pNode->setAttribute((_bstr_t)_T("BomDataBigNum"),(_variant_t)i);
pNode->setAttribute((_bstr_t)_T("BomDataSmallNum"),(_variant_t)ii);
pNode->setAttribute((_bstr_t)_T("key"),(_variant_t)strKey);
pNode->setAttribute((_bstr_t)_T("value"),(_variant_t)strValue);
pRoot->appendChild(pNode);
}
}
pDocXML->save( _bstr_t(strBomFile));
pNode->Release();
pRoot->Release();
pDocXML->Release();
Private Sub Command1_Click()
Dim xDoc As Object
Dim xmlFile As String
Dim strWidth As String
Dim strHeight As String
Set xDoc = CreateObject("MSXML2.DOMDocument")
xmlFile = "C:\aa.xml"
xDoc.Load xmlFile
strWidth = xDoc.selectSingleNode("overlays").selectSingleNode("Info").Attributes().Text
strHeight = xDoc.selectSingleNode("overlays").selectSingleNode("Info").Attributes().Text
MsgBox ("Width=" & strWidth & " Height=" & strHeight)
xDoc.selectSingleNode("overlays").selectSingleNode("Info").Attributes().Text = ""
xDoc.selectSingleNode("overlays").selectSingleNode("Info").Attributes().Text = ""
xDoc.save (xmlFile)
End Sub 注意 你上面的XML中Width=""Height="" 要分开,改成:
Width="" Height=""





