×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    11

    how to use XML parser

    I am following the link
    http://www.developer.nokia.com/Commu..._CParser_class
    have developed a code.

    But I think the link does not give a clear picture of how to use xml parser.

    I have implemented
    void OnStartDocumentL( const RDocumentParameters &aDocParam, TInt aErrorCode );

    void OnEndDocumentL( TInt aErrorCode );

    void OnStartElementL( const RTagInfo &aElement, const RAttributeArray &aAttributes,
    TInt aErrorCode );

    void OnEndElementL( const RTagInfo &aElement, TInt aErrorCode );

    void OnContentL( const TDesC8 &aBytes, TInt aErrorCode );

    I am getting data in OnContentL. But how do i know which datta belong to which element. and how do i use it then

  2. #2
    Nokia Developer Moderator
    Join Date
    Oct 2006
    Location
    Finland
    Posts
    4,561

    Re: how to use XML parser

    OnStartElement you will have found the tag name for example: "<tag>"


    use something like
    iTag = aElement.LocalName().DesC().AllocL();
    In OnContentL, the aBytes will contain the corresponding data to the tag above.

    Use debugger effectively here to see what each one is, but the broad idea is what I explained above.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    11

    Re: how to use XML parser

    It is 1st time I am doing xml parsing myself,
    I am in doubt if I am using the right approach
    My code looks like this

    void CXmlHandler::OnStartElementL( const RTagInfo &aElement, const RAttributeArray &aAttributes,
    TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
    {
    TBuf8<50>b(*(aElement.LocalName().DesC().Alloc()));
    iBuf=b;

    }
    else
    {
    //error

    }
    }

    void CXmlHandler::OnEndElementL( const RTagInfo &aElement, TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
    {
    TBuf8<50>b(*(aElement.LocalName().DesC().Alloc()));
    iBuf=b;
    iBuf;
    // Do something if needed....

    }
    else
    {
    //error



    }
    }

    void CXmlHandler::OnContentL( const TDesC8 &aBytes, TInt aErrorCode )
    {
    if ( KErrNone == aErrorCode )
    {
    if((!aBytes.Compare(_L8("\n")))||(!aBytes.Compare(_L8("\t"))))
    return;
    HBufC* buffer = HBufC::NewLC( aBytes.Length() + 4 );
    TPtr bufferPtr( buffer->Des() );
    bufferPtr.Copy( aBytes );
    if(!iBuf.Compare(_L8("PhoneBook")))
    {

    }
    if(!iBuf.Compare(_L8("name")))
    {
    bufferPtr.Append(_L("xyz")) ;
    }
    if(!iBuf.Compare(_L8("number")))
    {
    bufferPtr.Append(_L("000")) ;
    }
    gConsole = Console::NewL(KConsoleMessageDisplay,TSize(20,20));
    CleanupStack::PushL(gConsole);
    gConsole->Printf(*buffer);
    gConsole->Getch();
    CleanupStack::PopAndDestroy(gConsole);
    CleanupStack::PopAndDestroy();
    }
    else
    {
    //error
    }
    }
    and xml file is->
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <PhoneBook time="11/10/2012 8:07:46 am">
    SyncMl for PhoneBook
    <name>
    sandeep
    </name>
    <number>
    9711879354
    </number>
    <name>
    Tripathy
    </name>
    <number>
    3433331
    </number>
    </PhoneBook>

    Though my code works I have a doubt, it will not work with multiple heirarchy child elements
    last time I saw a code in linux, that was using a structure of elements in the parsing code, but I am not using.

    Hope to get some help writing a better code....

    Any pointer to an xml code that is using the parsed xml files would be great.

    Regards!
    Last edited by sandeepintg; 2012-10-11 at 14:15.

  4. #4
    Nokia Developer Moderator
    Join Date
    Oct 2006
    Location
    Finland
    Posts
    4,561

    Re: how to use XML parser

    AFAIK it should work for any standard XML file even having multiple hierarchy children.

    I do not understand your further question..if its working what more do you want. The API provided does the job, and thats the way it needs to be implemented (Ofcourse the code could have been a little cleaner, but for thats your preference

    btw, when you post code, use code tags for better readability.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    11

    Re: how to use XML parser

    1>I had earlier seen use of struct of elements in one of my previous projects,which was using XML Parser, that is why I was concerned.
    2>I am not getting the attribute details like
    time="11/10/2012 8:07:46 am"
    in xml file line
    <PhoneBook time="11/10/2012 8:07:46 am">
    3>what is "code tags " that you mentioned and how to use them?
    4> I am getting strings like "\t" and "\n" in OnContentL( const TDesC8 &aBytes, TInt aErrorCode )
    why so? is it ok?
    Last edited by sandeepintg; 2012-10-12 at 10:50.

  6. #6
    Nokia Developer Moderator
    Join Date
    Oct 2006
    Location
    Finland
    Posts
    4,561

    Re: how to use XML parser

    Quote Originally Posted by sandeepintg View Post
    1>I had earlier seen use of struct of elements in one of my previous projects,which was using XML Parser, that is why I was concerned.
    2>I am not getting the attribute details like
    time="11/10/2012 8:07:46 am
    in xml file line
    PhoneBook time="11/10/2012 8:07:46 am
    3>what is "code tags " that you mentioned and how to use them?
    4> I am getting strings like "\t" and "\n" in OnContentL( const TDesC8 &aBytes, TInt aErrorCode )
    why so? is it ok?
    1. ok
    2. If you see that is not wrapped by the tags, so you wouldnt get it via the parse (atleast I dont know if). Usually such things you can see by debugging line by line and see each tag/variable contents and adjust your code..That way you will see how its exactly handling the parsing
    3. use [CODE] [ /CODE] without the space ofcourse which I put there between '[' and '/' This is also available in the tools above if you 'go' into advanced editor.
    Code:
     Here you can put the code
    4. Answer similar to second part of 2. Probably your xml file contains newlines, etc. Again here advise is, debug and you would see what is happening there.

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,748

    Re: how to use XML parser

    It has been some time ago, but I had an XML example in the past, and now you can access it via http://folk.uio.no/gergelyc/Symbian - it may help a little, at least it is a complete one.

  8. #8
    Super Contributor
    Join Date
    Oct 2007
    Location
    જુનાગઢ - India
    Posts
    3,034

    Re: how to use XML parser

    2>I am not getting the attribute details like
    time="11/10/2012 8:07:46 am"
    in xml file line
    <PhoneBook time="11/10/2012 8:07:46 am">
    Basically RAttributeArray (Check OnStartElementL method) is a array of RAttribute which contain element name(here "time") and its value (here "11/10/2012 8:07:46 am"). In case you have more then one attribute in tag then just run a loop and get value of each item from RAttributeArray .

Similar Threads

  1. SDP Parser
    By nanchu in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2010-04-15, 06:49
  2. about the XML parser
    By neolcs in forum Symbian
    Replies: 3
    Last Post: 2009-11-30, 12:37
  3. kXML XML Parser versus Jsr172 API Parser
    By javame.developer in forum Mobile Java General
    Replies: 1
    Last Post: 2008-02-14, 20:23
  4. xhtml parser & xml parser
    By indvin in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2005-08-25, 13:56
  5. Parser
    By Ojak Dev Sharma in forum Mobile Java General
    Replies: 3
    Last Post: 2004-07-09, 22:37

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×