Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML Elements using TinyXML

UPDATE: Still not working :( I have updated the code portion to reflect what I currently have.

This should be a pretty easy question for people who have used TinyXML. I'm attempting to use TinyXML to parse through an XML document and pull out some values. I figured out how to add in the library yesterday, and I have successfully loaded the document (hey, it's a start).

I've been reading through the manual and I can't quite figure out how to pull out individual attributes. After Googling around, I haven't found an example of my specific example, so perhaps someone here who has used TinyXML can help out. Below is a slice of the XML, and where I have started to parse it.

XML:

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

   <card type="EGC2">
    <offsets>
      [ ... ]
    </offsets>
  </card>
</EGCs>

Loading/parsing code:

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 card "type" and save it to a string to put into a combobox. How do I access this attribute member?

like image 763
Jon Avatar asked Jun 29 '11 13:06

Jon


People also ask

How does parsing XML work?

XML parser is a software library or a package that provides interface for client applications to work with XML documents. It checks for proper format of the XML document and may also validate the XML documents. Modern day browsers have built-in XML parsers. The goal of a parser is to transform XML into a readable code.

Can XML parse HTML?

XML parsers will fail to parse any HTML document that uses any of those features. HTML parsers, on the other hand, will basically never fail no matter what a document contains.


3 Answers

After a lot of playing around with the code, here is the solution! (With help from 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;
}
like image 87
Jon Avatar answered Oct 17 '22 21:10

Jon


there should be a Attribute method that takes and attribut name as parameter see: http://www.grinninglizard.com/tinyxmldocs/classTiXmlElement.html

from the documentation I see the code would look like:

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

However for the type of thing you are doing I would use XPATH if at all possible. I have never used it but the TinyXPath project may be helpful if you choose to go that route the link is: http://tinyxpath.sourceforge.net/

Hope this helps.

The documentation I am using to help you from is found at: http://www.grinninglizard.com/tinyxmldocs/hierarchy.html

like image 2
Rob Avatar answered Oct 17 '22 23:10

Rob


What you need is to get the attribute type from the element card. So in your code it should be something like:

const char * attribval = hRoot.FirstChild("card").ToElement()->Attribute("card");
like image 1
iceaway Avatar answered Oct 17 '22 22:10

iceaway