GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Feb-2005, 05:06
njp01u njp01u is offline
New Member
 
Join Date: Jan 2005
Posts: 8
njp01u is on a distinguished road

Displaying node attributes in an XML tree display


I have successfully managed to display some XML code in a tree, but currently only the node names are given. I would also like to display any attributes (i.e. everything inside the node, not just the name) but can't for the life of me find out how! Please help - I appreciate your efforts very much!

CPP / C++ / C Code:
// This calls the BuildTree method with m_pDocRoot as an IXMLDOMElementPtr

// Initialize root pointer
m_pDocRoot = m_plDomDocument -> documentElement;

// Delete previous tree structure
VERIFY(m_Tree.DeleteAllItems());
// Display new tree structure
HTREEITEM hxmlRoot = TVI_ROOT;
this -> BuildTree(this -> m_pDocRoot, &(this -> m_Tree), hxmlRoot);
------------------------------------------------------------------------------------------------------------
// Construct XML tree structure
void View_Tree::BuildTree(IXMLDOMNodePtr pParent, CTreeCtrl *m_Tree, HTREEITEM hParent) {

// Insert node into tree
HTREEITEM hChild;
InsertNode(pParent, m_Tree, hParent, hChild);

// Recursive call to insert each node into the tree in turn
for (IXMLDOMNodePtr pChild = pParent -> firstChild; NULL != pChild; pChild = pChild -> nextSibling) {

BuildTree(pChild, m_Tree, hChild);

}

} // End of BuildTree()
--------------------------------------------------------------------------------------------------------
// Insert specified node into tree structure
void View_Tree::InsertNode(IXMLDOMNodePtr pParent, CTreeCtrl *m_Tree, HTREEITEM hParent, HTREEITEM &hChild) {

// Leaf node
if (NODE_TEXT == pParent -> nodeType) {

// Enter child node into the tree
m_Tree -> InsertItem(pParent -> text, hParent, TVI_LAST);

// Parent node
} else {

// Enter parent node into the tree and define its children
hChild = m_Tree -> InsertItem(pParent -> nodeName, hParent, TVI_LAST);

}

} // End of InsertNode()

I know the 'hChild = m_Tree -> InsertItem(pParent -> nodeName, hParent, TVI_LAST);' line is where the text for the tree comes from but I don't know what to use in place of nodeName.

I hope this all makes sense. Thanks again!
Last edited by LuciWiz : 07-Feb-2005 at 05:19. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 07-Feb-2005, 06:20
njp01u njp01u is offline
New Member
 
Join Date: Jan 2005
Posts: 8
njp01u is on a distinguished road
Smile

It's alright - I've just managed to sort it. This needs to be done for each node if anyone has had the same problem:
--------------------------------------------------------------------------
// Create object containing attributes
IXMLDOMNamedNodeMap* attr;
HRESULT hr;
hr = pParent->get_attributes(&attr);

_bstr_t node_properties;
node_properties += pParent->nodeName;

// Add each attribute in the node to a string
if (attr != NULL) {
for (int i = 0; i < attr->length; i++) {
IXMLDOMNode* node;
attr->get_item(i,&node);
node_properties += (" " + *&node->nodeName + "=\"" + *&node->text + "\"");
}
}

// Add string into tree
hChild = m_Tree -> InsertItem(node_properties, hParent, TVI_LAST);
--------------------------------------------------------------------------
  #3  
Old 07-Feb-2005, 18:42
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
just an addition reference...

here is an excellent wrappper for the xml document element, i found it some where in the net before, don't remember where though...

CPP / C++ / C Code:
// TElem -- a simple class to wrap up IXMLDomElement and iterat its children.
//   name()    - in <item>stuff</item> it returns "item"
//   val()     - in <item>stuff</item> it returns "stuff"
//   attr(s)   - in <item s=L"hello">stuff</item> it returns "hello"
//   subnode(b)- in <item><a>hello</a><b>there</b></item> it returns the TElem <b>there</b>
//   subval(b) - in <item><a>hello</a><b>there</b></item> it returns "there"
//   for (TElem c=e.begin(); c!=e.end(); c++) {...} - iterators over the subnodes
struct TElem
{ 
	
  CComPtr<IXMLDOMElement> elem;
  CComPtr<IXMLDOMNodeList> nlist; int pos; long clen;
  //
  TElem() : elem(0), nlist(0), pos(-1), clen(0) {}
  TElem(int _clen) : elem(0), nlist(0), pos(-1), clen(_clen) {}
  TElem(CComPtr<IXMLDOMElement> _elem) : elem(_elem), nlist(0), pos(-1), clen(0) {get();}
  TElem(CComPtr<IXMLDOMNodeList> _nlist) : elem(0), nlist(_nlist), pos(0), clen(0) {get();}
  void get()
  { if (pos!=-1)
    { elem=0;
      CComPtr<IXMLDOMNode> inode;
      nlist->get_item(pos,&inode);
      if (inode==0) return;
      DOMNodeType type; inode->get_nodeType(&type);
      if (type!=NODE_ELEMENT) return;
      CComQIPtr<IXMLDOMElement> e(inode);
      elem=e;
    }
    clen=0; if (elem!=0)
    { CComPtr<IXMLDOMNodeList> iNodeList;
      elem->get_childNodes(&iNodeList);
      iNodeList->get_length(&clen);  
    }
  }
  //
  wstring name() const
  { if (!elem) return L"";
    CComBSTR bn; elem->get_tagName(&bn);
    return wstring(bn);
  }
  wstring attr(const wstring name) const
  { if (!elem) return L"";
    CComBSTR bname(name.c_str());
    CComVariant val(VT_EMPTY);
    elem->getAttribute(bname,&val);
    if (val.vt==VT_BSTR) return val.bstrVal;
    return L"";
  }
  bool attrBool(const wstring name,bool def) const
  { wstring a = attr(name);
    if (a==L"true" || a==L"TRUE") return true;
    else if (a==L"false" || a==L"FALSE") return false;
    else return def;
  }
  int attrInt(const wstring name, int def) const
  { wstring a = attr(name);
    int i, res=swscanf(a.c_str(),L"%i",&i);
    if (res==1) return i; else return def;
  }
  wstring val() const
  { if (!elem) return L"";
    CComVariant val(VT_EMPTY);
    elem->get_nodeTypedValue(&val);
    if (val.vt==VT_BSTR) return val.bstrVal;
    return L"";
  }
  TElem subnode(const wstring name) const
  { if (!elem) return TElem();
    for (TElem c=begin(); c!=end(); c++)
    { if (c.name()==name) return c;
    }
    return TElem();
  }
  wstring subval(const wstring name) const
  { if (!elem) return L"";
    TElem c=subnode(name);
    return c.val();
  }
  TElem begin() const
  { if (!elem) return TElem();
    CComPtr<IXMLDOMNodeList> iNodeList;
    elem->get_childNodes(&iNodeList);
    return TElem(iNodeList);
  }
  TElem end() const
  { return TElem(clen);
  }
  TElem operator++(int)
  { if (pos!=-1) {pos++; get();}
    return *this;
  }
  bool operator!=(const TElem &e) const
  { return pos!=e.clen;
  }
};

__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ineed some help with thos code - Linear Linked List sosy2001 C Programming Language 6 11-Nov-2004 11:23
Linked List Kareem1984 C Programming Language 5 07-Oct-2004 18:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.