Parsing XML Elements Using TinyXML

UPDATE: still not working :( I updated part of the code to reflect what I have.

This should be a fairly straightforward question for people who have used TinyXML. I am trying to use TinyXML to parse an XML document and highlight some values. I figured out how to add to the library yesterday, and I successfully uploaded the document (hey, this is the beginning).

I read the manual and I cannot figure out how to pull out individual attributes. After Googling, I did not find an example of my specific example, so maybe someone who used TinyXML might help. The XML snippet is presented below, and I started parsing it.

XML:

<EGCs xmlns="http://tempuri.org/XMLSchema.xsd"> <card type="EGC1"> <offsets> [ ... ] </offsets> </card> <card type="EGC2"> <offsets> [ ... ] </offsets> </card> </EGCs> 

Download / code analysis:

 TiXmlDocument doc("EGC_Cards.xml"); if(doc.LoadFile()) { TiXmlHandle hDoc(&doc); TiXmlElement* pElem; TiXmlHandle hRoot(0); pElem = hDoc.FirstChildElement().Element(); if (!pElem) return false; hRoot = TiXmlHandle(pElem); //const char *attribval = hRoot.FirstChild("card").ToElement()->Attribute("card"); pElem = hDoc.FirstChild("EGCs").Child("card", 1).ToElement(); if(pElem) { const char* tmp = pElem->GetText(); CComboBox *combo = (CComboBox*)GetDlgItem(IDC_EGC_CARD_TYPE); combo->AddString(tmp); } } 

I want to pull out each type map and save it in a row to insert into the combo box. How to access this attribute element?

+6
source share
3 answers

After talking to the code many times, here is the solution! (Using HERE )

 TiXmlDocument doc("EGC_Cards.xml"); combo = (CComboBox*)GetDlgItem(IDC_EGC_CARD_TYPE); if(doc.LoadFile()) { TiXmlHandle hDoc(&doc); TiXmlElement *pRoot, *pParm; pRoot = doc.FirstChildElement("EGCs"); if(pRoot) { pParm = pRoot->FirstChildElement("card"); int i = 0; // for sorting the entries while(pParm) { combo->InsertString(i, pParm->Attribute("type")); pParm = pParm->NextSiblingElement("card"); i++; } } } else { AfxMessageBox("Could not load XML File."); return false; } 
+8
source

there must be an Attribute method that accepts and assigns a name as a parameter: http://www.grinninglizard.com/tinyxmldocs/classTiXmlElement.html

from the documentation that I see will look like this:

  hRoot.FirstChildElement("card").ToElement()->Attibute("type"); 

However, for what you are doing, I would use XPATH, if at all possible. I have never used it, but the TinyXPath project may be useful if you decide to go this route: http://tinyxpath.sourceforge.net/

Hope this helps.

The documentation that I use to help you is located at: http://www.grinninglizard.com/tinyxmldocs/hierarchy.html

+2
source

You need to get the type attribute from the card element. So your code should have something like:

 const char * attribval = hRoot.FirstChild("card").ToElement()->Attribute("card"); 
+1
source

Source: https://habr.com/ru/post/891639/


All Articles