CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    TiXmlElement (tinyxml), looking for ways to get "Next" Child?

    Good Morning,
    Code:
    <a>
      <b>b</b>
      <c>c</c>
    </a>
    TiXmlElement* b = a->FirstChildElement();
    TiXmlElement* c = b->NextSiblingElement();

    How can I get "c" directly from "a", without prior knowledge of "b" in the middle
    Like
    TiXmlElement* c = a->NextChildElement(); which is not available in tinyxml
    Any help is greatly appreciated!
    Thanks
    Jack

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: TiXmlElement (tinyxml), looking for ways to get "Next" Child?

    You could always write:
    Code:
    TiXmlElement* c = a->FirstChildElement()->NextSiblingElement();
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902

    Re: TiXmlElement (tinyxml), looking for ways to get "Next" Child?

    Or a->FirstChild("c") if the schema is that rigid.

    I prefer tinyxml2 myself.

    gg

  4. #4
    Join Date
    Dec 2010
    Posts
    907

    Re: TiXmlElement (tinyxml), looking for ways to get "Next" Child?

    Thanks, I've got an additional question which is quite off-topic.
    Code:
    <Index id="0">0</Index>
    <Index id="1">2</Index>
    <Index id="2">1</Index>
    If I want to represent this, not by ids, but a size number (of 3) followed by
    3 numbers, what is the recommended way to represent this in xml?
    Thanks
    Jack

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: TiXmlElement (tinyxml), looking for ways to get "Next" Child?

    there is no "recommended" way if what you want to do it represent an array then there are various ways how you COULD do it, with each their advantages and disadvantages.

    1) name the array and reuse it for items, using internal xml ordering as the index (= the collection of items is the array itself)
    Code:
       <MyArray>0</MyArray>
       <MyArray>2</MyArray>
       <MyArray>1</MyArray>
    2) name the array, and enumerate the elements explicitely, using internal xml ordering as the index (=the collection is a container of nodes)
    Code:
       <MyArray>
          <item>0</item>
          <item>2</item>
          <item>1</item>
       </MyArray>
    3) Either 1 or 2 with explicit index attributes (this is what you used in #4 applied to 1)).
    This has the advantage over 1 and 2 that you are not dependant on the internal xml ordering and you can leave gaps in the index numbering. Either of which can be an advantage or disadvantage.

    4) using indexed tags (applicable on the 1 or 2 method, though it's rare to see it used on 1)
    Code:
       <MyArray>
          <item0>0</item0>
          <item1>2</item1>
          <item2>1</item2>
       </MyArray>
    5) using your own parsing on the item. (lets say a semiconol separated list of values)
    Code:
       <MyArray>0;2;1</MyArray>
    6) ... Whatever other system works for you.



    There's no 'right' or 'wrong' way to do things, I've seen all of the above being used. Each method has pro's and cons.

    There is a growing trend in xml however (partly driven by xpath syntax, XSD syntax and xml tools) to view nodes in xml as containers for what's in side them. Given that paradigm, the typical solution for an array is given in 2) whereby you can create a definition for an item (which could be a simple type or a complex type), and then create a complextype for the array of that type of item.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured