Thread: C++ Parser Code Problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    C++ Parser Code Problem

    Can anyone assist with this Xerces/C++ parser? I keep getting an access violation and a bad pointer error after debugging. Not sure what I am doing wrong. I’m trying to create an XML C++ parser. With this code snippet, there are no build or compile errors, however on the commandline, after entering the filename, I get the access violation error. I would appreciate any insight to this problem.

    Code:
    	SAX2XMLReader* parser1 = XMLReaderFactory::createXMLReader();
    	DefaultHandler* dHandler = new DefaultHandler();
    	parser1->setContentHandler(dHandler);
    	parser1->setErrorHandler(dHandler);
       
    	try
    	{
    		const Attributes* myAtts = new VecAttributesImpl();
    		const Attributes & atts = (Attributes&) myAtts;
    				
    		const unsigned int attsIdx=0;
    		//Bad pointer Error here at qname1
    		const XMLCh *const qname1 = atts.getQName(attsIdx);//constant pointer
    		const XMLCh *const lname = atts.getLocalName(attsIdx);//constant pointer
    		const XMLCh *const uri = atts.getURI(attsIdx);
    
    		parser1->parse(xmlFile);//class pointer to a virtual method
    		dHandler->startDocument();
    		dHandler->startElement(uri,lname,qname1,atts);		
    	for(int i =0;i<atts.getLength(); i++)
          {
    		XERCES_STD_QUALIFIER cout << xmlFile << ": " <<  " Ready "
    		<< "\n" << "Q Name : " << atts.getQName(i) << "\n"
    		<< "\n" << "L Name : " << lname << "\n"
    << "Attributes Retrieved" <<"\n"<<"\n"<<"\n"<<"\n" << XERCES_STD_QUALIFIER endl;
    				return 1;
    	}//end for
    	
    	}//end try
    	.
    .
    .
    Last edited by pipercubusa; 03-15-2005 at 01:12 PM.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    My suggestion is to let us know what IDE you are using, and then someone can give you suggestions on how to use the debugger, so that you can step through the code line by line. Things should then become obvious.

    That said, this looks kind of weird to me:

    Code:
        const Attributes* myAtts = new VecAttributesImpl();
        const Attributes & atts = (Attributes&) myAtts;
    I could be wrong, but I don't think you can just cast a pointer to be a reference and then go about your merry way.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    I am using Microsoft Visual C++ 7.1. IDE

    The Attributes class is an abstract class, whereas VecAttributesImpl() is the implementation class for the Attributes class. I used the -- new VecAtributesImpl() in order to create the Attributes class....had I not, I would receive an error stating an abstract class can not be instantiated. If you have another idea on how to instantiate an abstract class, I am open for suggestions.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    That wasn't really the point. What I meant was, that after you have instantiated the class, you are casting a pointer to a reference, and then using the reference to access the class. I don't think you can do this (a reference isn't the same thing as a pointer, although they do similar things). I think you have two choices:

    Code:
     const Attributes* myAtts = new VecAttributesImpl();
    
     // dereference the pointer if you want to use a reference
     const Attributes & atts = *myAtts;
    
     // or don't use the reference: just use the pointer to access 
     // myAtts, for instance myAtts->getQName(attsIdx);
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Thanks.....that was a big help.

    I used:
    Code:
    const Attributes* myAtts = new VecAttributesImpl();
    const Attributes & atts = *myAtts;
    I no longer received the "Access Violation" error.
    However I am having problems with the For loop printing the data from the XML file.
    Here is the For loop I am using.
    Code:
    for(unsigned int i =0;  i<4; i++)
         {
           XERCES_STD_QUALIFIER cout << xmlFile << ": " <<  " Ready "
           << "\n" << "Q Name : " << myAtts->getQName(i) << "\n"
           << "Attributes Retrieved" <<"\n"<<"\n"<<"\n"<<"\n" << XERCES_STD_QUALIFIER endl;
           return 1;
          }//end for
    What do you recommend?

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I'm not certain. What is the data type of xmlFile? What does XERCES_STD_QUALIFIER do? What is the return type of getQName()? What type of output do you expect to see? What output are you actually seeing? Also, does this library have a help page? Usually, there are code examples there that show how to do the basics, i.e. print out the values.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    There is no simple solution to this problem as I thought it would be. Can anyone help please?
    I am trying to print out the names of the elements and the attributes. Here are the files and code I have. The IDE is MSVC++ 7.1

    Sax2count.cpp main file:
    Code:
    const Attributes* myAtts = new VecAttributesImpl();
    const Attributes & atts = *myAtts;
    
    const XMLCh *const eleName = new XMLCh;//not sure what to do with the XMLCh
    
    handler.startDocument();
    handler.startElement(eleName,attributes);
    
    //this prints element names to console(xerces unique print method:same as cout)
    XERCES_STD_QUALIFIER cout << xmlFile << ": " <<" START" << " ms ("
    << handler.getElementName()<< " Element Name, "<< XERCES_STD_QUALIFIER endl;
    Handler files:
    Sax2countHandlers.cpp file:
    Code:
    //fElementName defined in const base class
    SAX2CountHandlers::SAX2CountHandlers() :
        fElementCount(0)
    	, fAttrCount(0)
    	, fElementName(0)//here
    		.
                                    .
                                    .
    --------------------------------------------
    void SAX2CountHandlers::startElement(const XMLCh *const eleName,
                               AttributeList & attributes)
    {
     char* message = XMLString::transcode(eleName);
        XERCES_STD_QUALIFIER cerr << "I saw element: "<< message <<        
        XERCES_STD_QUALIFIER endl;
        XMLString::release(&message);
    }
    Sax2countHandlers.hpp file:
    Code:
    public:
    
    // -----------------------------------------------------------------------
    //  Getter methods
    // -----------------------------------------------------------------------
        const XMLCh getElementName() const
        {
            return fElementName;
        }
    
    // -----------------------------------------------------------------------
    //  Handlers for the SAX ContentHandler interface
    // -----------------------------------------------------------------------
    void startElement(const XMLCh* const eleName,  AttributeList& attributes);
    
    //under private
    private:
      const XMLCh  fElementName;
    Console output:
    I saw element:========================================== ===================

    Xml file sample:
    <Scenarios>
    <Signal userName="Richard">
    <ScenarioName>PMOP-OS</ScenarioName>
    <RadioFrequency>3</RadioFrequency>
    <PRF1>33</PRF1>
    <StartFrequency>3</StartFrequency>
    <StopFrequency>3</StopFrequency>
    <ControlNumber>1002</ControlNumber>
    </Signal>
    <Signal userName="Mark">
    <ScenarioName>PMOP-OS</ScenarioName>
    <RadioFrequency>3</RadioFrequency>
    <PRF1>33</PRF1>
    <StartFrequency>3</StartFrequency>
    <StopFrequency>3</StopFrequency>
    <ControlNumber>1002</ControlNumber>
    </Signal>
    .
    .
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  2. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM