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. How to get a specified attribute value using MSXML4

How to get a specified attribute value using MSXML4

Scheduled Pinned Locked Moved XML / XSL
xmltutorialquestionannouncement
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.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    How to get the value of "hw" and "pos", xml file:

    <?xml version="1.0" ?>
    <Root>
    <w c5="NN1" hw="factsheet" pos="SUBST">FACTSHEET </w>
    <w c5="DTQ" hw="what" pos="PRON">WHAT </w>
    <w c5="VBZ" hw="be" pos="VERB">IS </w>
    <w c5="NN1" hw="aids" pos="SUBST">AIDS</w>
    <w c5="NN1" hw="aids" pos="SUBST">AIDS </w>
    <w c5="VVN-AJ0" hw="acquire" pos="VERB">Acquired </w>
    </Root>

    Here is a piece of code:

    MSXML2::IXMLDOMDocumentPtr pDoc;
    HRESULT hr;
    hr = pDoc.CreateInstance(\_\_uuidof(MSXML2::DOMDocument40));
    if (FAILED(hr))	{
    	MessageBox("err");
    	return;
    }
    pDoc->load("d:\\\\a01.xml");
    MSXML2::IXMLDOMNodeListPtr pNodeList;
    MSXML2::IXMLDOMNode \*DOMNode=NULL;
    CString strContest;
    try {
        pNodeList=pDoc->getElementsByTagName("w");
    	int j=pNodeList->length;
        for(int i=0;i<pNodeList->length;++i) {
    

    ///////////////////////////////////////////////////////////////
    //Here, get the item value, how to get a specified attribute value
    pNodeList->get_item(i,&DOMNode);
    strContest=(LPCSTR)DOMNode->Gettext();
    MessageBox(strContest);
    }
    } catch(_com_error &err) {
    CString strErr=(LPCTSTR)err.Description();
    MessageBox(strErr);
    }

    CPalliniC 1 Reply Last reply
    0
    • F followait

      How to get the value of "hw" and "pos", xml file:

      <?xml version="1.0" ?>
      <Root>
      <w c5="NN1" hw="factsheet" pos="SUBST">FACTSHEET </w>
      <w c5="DTQ" hw="what" pos="PRON">WHAT </w>
      <w c5="VBZ" hw="be" pos="VERB">IS </w>
      <w c5="NN1" hw="aids" pos="SUBST">AIDS</w>
      <w c5="NN1" hw="aids" pos="SUBST">AIDS </w>
      <w c5="VVN-AJ0" hw="acquire" pos="VERB">Acquired </w>
      </Root>

      Here is a piece of code:

      MSXML2::IXMLDOMDocumentPtr pDoc;
      HRESULT hr;
      hr = pDoc.CreateInstance(\_\_uuidof(MSXML2::DOMDocument40));
      if (FAILED(hr))	{
      	MessageBox("err");
      	return;
      }
      pDoc->load("d:\\\\a01.xml");
      MSXML2::IXMLDOMNodeListPtr pNodeList;
      MSXML2::IXMLDOMNode \*DOMNode=NULL;
      CString strContest;
      try {
          pNodeList=pDoc->getElementsByTagName("w");
      	int j=pNodeList->length;
          for(int i=0;i<pNodeList->length;++i) {
      

      ///////////////////////////////////////////////////////////////
      //Here, get the item value, how to get a specified attribute value
      pNodeList->get_item(i,&DOMNode);
      strContest=(LPCSTR)DOMNode->Gettext();
      MessageBox(strContest);
      }
      } catch(_com_error &err) {
      CString strErr=(LPCTSTR)err.Description();
      MessageBox(strErr);
      }

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You have to use the attributes property (i.e the get_attributes method) and then the getNamedItem on the returned NodeMap, check out the C++ sample of the getNamedItem documentation: http://msdn2.microsoft.com/en-us/library/ms767592.aspx[^] it does almost exactly what you need (it retrieves an attribute of the documentElement, but the same applies to a standard node). Please note that you can also do a more intensive use of the smart pointers to make code more concise. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      [my articles]

      In testa che avete, signor di Ceprano?

      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