Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. XML MFC/C++

XML MFC/C++

Scheduled Pinned Locked Moved XML / XSL
c++helpcomxmlquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vikas K
    wrote on last edited by
    #1

    Hi, I have created a simple MFC/C++ Program that uses MS XML 3.0 and Below is the code as well as XML file. Its a very smal XML file and I need to parse it and get info like ID = "DEV01" Name = "robotest" etc But, I am just getting tag names and I am not getting Values :( Can anyone point out where I am going wrong ??? XMLPad.exe parses it properly !! Help.. XML file is: ////////////// MFC Code (COM)//////////////// #import named_guids using namespace MSXML; IXMLDOMDocumentPtr m_plDomDocument; //pointer to the xml document IXMLDOMElementPtr m_pDocRoot; //pointer to the root ::AfxOleInit(); ::CoInitialize(NULL); HRESULT hr = m_plDomDocument.CreateInstance(MSXML::CLSID_DOMDocument); if(FAILED(hr)) { AfxMessageBox("Error!!"); } ... ... CFileDialog fd(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_FILEMUSTEXI ST,"XML Files (*.xml)|*.xml||", this); fd.m_ofn.lpstrTitle = "Choose XML File"; if(fd.DoModal()==IDOK) { CString file = fd.GetPathName(); _bstr_t filename = file.AllocSysString(); variant_t vResult; vResult = m_plDomDocument->load(filename); if((bool)vResult==TRUE) { m_pDocRoot = m_plDomDocument->documentElement; AfxMessageBox("Document Loaded !!"); AfxMessageBox(m_pDocRoot->xml); HTREEITEM hxmlRoot = TVI_ROOT; BuildTree(m_pDocRoot, &m_tree, hxmlRoot); } else { AfxMessageBox("Document Failed!!"); } } ..... void CXMLDialog::BuildTree(IXMLDOMNodePtr pParent, CTreeCtrl *m_pTree, HTREEITEM hParent) { HTREEITEM hChild; InsertNode(pParent, m_pTree, hParent, hChild); for(IXMLDOMNodePtr pChild = pParent->firstChild; NULL != pChild; pChild = pChild->nextSibling) { BuildTree(pChild,m_pTree,hChild); } } ... void CXMLDialog::InsertNode(IXMLDOMNodePtr pParent, CTreeCtrl *m_pTree, HTREEITEM hParent, HTREEITEM &hChild) { if(pParent->nodeType == MSXML::NODE_TEXT) { m_pTree->InsertItem(pParent->text, hParent, TVI_LAST); } else { hChild = m_pTree->InsertItem(pParent->nodeName, hParent, TVI_LAST); } }

    R 1 Reply Last reply
    0
    • V Vikas K

      Hi, I have created a simple MFC/C++ Program that uses MS XML 3.0 and Below is the code as well as XML file. Its a very smal XML file and I need to parse it and get info like ID = "DEV01" Name = "robotest" etc But, I am just getting tag names and I am not getting Values :( Can anyone point out where I am going wrong ??? XMLPad.exe parses it properly !! Help.. XML file is: ////////////// MFC Code (COM)//////////////// #import named_guids using namespace MSXML; IXMLDOMDocumentPtr m_plDomDocument; //pointer to the xml document IXMLDOMElementPtr m_pDocRoot; //pointer to the root ::AfxOleInit(); ::CoInitialize(NULL); HRESULT hr = m_plDomDocument.CreateInstance(MSXML::CLSID_DOMDocument); if(FAILED(hr)) { AfxMessageBox("Error!!"); } ... ... CFileDialog fd(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_FILEMUSTEXI ST,"XML Files (*.xml)|*.xml||", this); fd.m_ofn.lpstrTitle = "Choose XML File"; if(fd.DoModal()==IDOK) { CString file = fd.GetPathName(); _bstr_t filename = file.AllocSysString(); variant_t vResult; vResult = m_plDomDocument->load(filename); if((bool)vResult==TRUE) { m_pDocRoot = m_plDomDocument->documentElement; AfxMessageBox("Document Loaded !!"); AfxMessageBox(m_pDocRoot->xml); HTREEITEM hxmlRoot = TVI_ROOT; BuildTree(m_pDocRoot, &m_tree, hxmlRoot); } else { AfxMessageBox("Document Failed!!"); } } ..... void CXMLDialog::BuildTree(IXMLDOMNodePtr pParent, CTreeCtrl *m_pTree, HTREEITEM hParent) { HTREEITEM hChild; InsertNode(pParent, m_pTree, hParent, hChild); for(IXMLDOMNodePtr pChild = pParent->firstChild; NULL != pChild; pChild = pChild->nextSibling) { BuildTree(pChild,m_pTree,hChild); } } ... void CXMLDialog::InsertNode(IXMLDOMNodePtr pParent, CTreeCtrl *m_pTree, HTREEITEM hParent, HTREEITEM &hChild) { if(pParent->nodeType == MSXML::NODE_TEXT) { m_pTree->InsertItem(pParent->text, hParent, TVI_LAST); } else { hChild = m_pTree->InsertItem(pParent->nodeName, hParent, TVI_LAST); } }

      R Offline
      R Offline
      Retsof Nawor
      wrote on last edited by
      #2

      Attributes aren't part of the child tree as you are trying to use it. Attributes are considered separate by the DOM and need to be accessed via IXMLDOMElement::getAttribute or IXMLDOMNode::attributes, with the later being best for you usage as you can iterate over all available attributes.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups