help loading an XML file using TinyXML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • modykun
    New Member
    • Apr 2010
    • 1

    help loading an XML file using TinyXML

    Reading an XML here is a must for my need
    I have to say though that i don't have any previous XML experience , so I'm not sure how to make it read it

    I'm using TinyXML , because i need fast code just to load the information from an XML file .
    so anyway , this is how my XML file looks like:
    Code:
    <?xml version="1.0" ?>
    <Rects>
        <Rect x="0" y="235" w="183" h="32" /> 
        <Rect x="246" y="236" w="266" h="42" />
    </Rects>
    as "Rects" is the root object and "Rect" are elements that i need to load and put in a vector structure , then draw each rectangle to the screen
    from some examples i found online , i gathered the following function to load the info and put it in a vector:
    Code:
    void LoadRectFile(const char* pFilename)
    {
    
    	TiXmlDocument doc(pFilename);
    
    	if (!doc.LoadFile()) return;
    
    	TiXmlHandle hDoc(&doc);
    	TiXmlElement* pElem;
    	TiXmlHandle hRoot(0);
    		// block: name
       
    		pElem=hDoc.FirstChildElement("Rects").Element();
    		// should always have a valid root but handle gracefully if it does
    		if (!pElem) return;
    
    
    		 // save this for later
    	 	hRoot=TiXmlHandle(pElem);
        
    		TiXmlElement* pRectNode=hRoot.FirstChild( "Rect" ).FirstChild().Element();
    		for( pRectNode; pRectNode; pRectNode=pRectNode->NextSiblingElement())
    	    	{
    			 Rectangle acRect( );
    			
    			 pRectNode->QueryIntAttribute((const char*)"x", (int*) &acRect.x); // If this fails, original value is left as-is
    			 pRectNode->QueryIntAttribute((const char*)"y",  (int*)&acRect.y);
    			 pRectNode->QueryIntAttribute((const char*)"w",(int*)  &acRect.width);
    			 pRectNode->QueryIntAttribute((const char*)"h", (int*) &acRect.height);
     
    	  		 clsRects.push_back(acRect);
    	    	}
    
    }
    but it's not working
    (Rectangle is a rectangle class that stores rectangles info by the way (like X,Y,Width,Heigh t for example ) )
    thanks in advance.
Working...