Menu

#535 XML parsing: Need sample C++ code to read nested Related nodes

Test Required
closed-invalid
None
5
2016-05-11
2016-05-11
No

Hi John,
I have a query regarding an use case of nested relation of nodes i.e. parent node has child, where child has parent node in it(<parent><child><parent></parent></child></parent>). Since expat is event based xml parser, I am new to use this library; now I would like to read and store the nested related elements data of a given xml. For example the structure of the xml is as shown below; In the below given sample xml data, <recipient> node is parent of <forwardto> node, where <forwardto> node is parent of <recipient> again.
Can Expat handle this scenario and reads out the data ? If yes, kindly suggest me with sample code using C++.</recipient></forwardto></forwardto></recipient>

<Recipient DeliveryType = "BCC">
        <UserInfo>
        <FirstName>Mahadev</FirstName>
        <LastName>Kanike</LastName>
        <FirmNumber>1219</FirmNumber>
        <AccountName>IND</AccountName>
        <AccountNumber>22222</AccountNumber>
        </UserInfo>
    <ForwardedTo>
        <Recipient DeliveryType = "BCC">
            <UserInfo>
            <FirstName>NICK</FirstName>
            <LastName>Dan</LastName>
            <AccountName>UK</AccountName> 
             <AccountNumber>11111</AccountNumber>
            </UserInfo>
        </Recipient>
    </ForwardedTo>
</Recipient>

Thanks in advance!

Best Regards,
Mahadev Kanike

Discussion

  • Sebastian Pipping

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -2,25 +2,25 @@
     I have a query regarding an use case of nested relation of nodes i.e. parent node has child, where child has parent node in it(<parent><child><parent></parent></child></parent>). Since expat is event based xml parser, I am  new to use this library; now I would like to read and store the nested related elements data of a given xml. For example the structure of the xml is as shown below; In the below given sample xml data, <Recipient> node is parent of <ForwardTo> node, where <Forwardto> node is parent of <Recipient> again.
     Can Expat handle this scenario and reads out the data ? If yes, kindly suggest me with sample code using C++.
    
    -<Recipient DeliveryType = "BCC">
    
    -        <UserInfo>
    -        <FirstName>Mahadev</FirstName>
    -        <LastName>Kanike</LastName>
    -        <FirmNumber>1219</FirmNumber>
    -        <AccountName>IND</AccountName>
    -        <AccountNumber>22222</AccountNumber>
    -        </UserInfo>
    -    <ForwardedTo>
    -        <Recipient DeliveryType = "BCC">
    +    <Recipient DeliveryType = "BCC">
                 <UserInfo>
    -            <FirstName>NICK</FirstName>
    -            <LastName>Dan</LastName>
    -            <AccountName>UK</AccountName> 
    -             <AccountNumber>11111</AccountNumber>
    +            <FirstName>Mahadev</FirstName>
    +            <LastName>Kanike</LastName>
    +            <FirmNumber>1219</FirmNumber>
    +            <AccountName>IND</AccountName>
    +            <AccountNumber>22222</AccountNumber>
                 </UserInfo>
    -        </Recipient>
    -    </ForwardedTo>
    -</Recipient>
    +        <ForwardedTo>
    +            <Recipient DeliveryType = "BCC">
    +                <UserInfo>
    +                <FirstName>NICK</FirstName>
    +                <LastName>Dan</LastName>
    +                <AccountName>UK</AccountName> 
    +                 <AccountNumber>11111</AccountNumber>
    +                </UserInfo>
    +            </Recipient>
    +        </ForwardedTo>
    +    </Recipient>
    
     Thanks in advance!
    
     
  • Sebastian Pipping

    Expat does support files like that but building a tree structure during parser is in your hands.
    I would expect classes close to

    class Recipient {
        .......
    private:
        UserInfo userInfo;
        Recipient * forwardTo;
    }
    
    class UserInfo {
        std::string firstName;
        std::string lastName;
        ......
    }
    

    in your code and a stack of pointers to instances of Recipient during parsing. In the handlers, you build and walk that stack and create Recipients as you go. I hope that gets you started.

     
  • Sebastian Pipping

    • status: open --> closed-invalid
     
  • Sebastian Pipping

    PS: For more support please contact me at firstname@lastname.org, please. I'll close this ticket as not-a-bug now.

     

Log in to post a comment.

MongoDB Logo MongoDB