Menu

Data types for XML elements

DavidA
2005-06-10
2013-05-20
  • DavidA

    DavidA - 2005-06-10

    Hi,

    I am a new user to XML and TinyXml.  I am using XML to define a parameter file to load and use in a C++ programme.

    I have a basic XML file as follows:

    <?xml version='1.0' ?>
    <network>
        <parameters>
            <param>
                <name>paramA</name>
                <value>-1</value>
            </param>
            <param>
                <name>paramB</name>
                <value>1</value>
            </param>
            <param>
                <name>paramC</name>
                <value>2.5</value>
            </param>
        </parameters>
    </network>

    I have used TinyXml to load the file successfully and have been able to extract the parameters names and values.  The type of the "name" and "value" elements of the "param" element is TEXT.  Is there a way of setting the value for paramA to be an integer, the value for paramB to be an unsigned integer and the value for paramC to be a double?  I have read that in an XML schema you can define the types of particular fields.

      Also would it be better to use attributes for this type of file? E.g.

    <?xml version='1.0' ?>
    <network>
        <parameters>
            <param paramA = '1'></param>
            <param paramB = '-1'></param>
            <param paramC = '2.5'></param>
        </parameters>
    </network>

    Any help will be gratefully received.

    David

     
    • Yves Berquin

      Yves Berquin - 2005-06-15

      XML itself doesn't define different types for the text and attributes. As far as XML is concerned, they are all strings.
      It is your choice, as a designer of the XML structure, to decide what will go into each node.
      As far as the choice between text node and attributes is concerned, I would suggest using text nodes every time there can be advanced text in it. Escaping quotes, for example, is not needed in text nodes.
      If you only need to add numerical attributes, you can certainly use element attributes :
      <parameters A='1' B='-1' C='2.5' />
      in your example works fine.

      Yves

       
    • feraradam

      feraradam - 2005-10-10

      hello dave,
      can you please show, how did you get the values and assigned them to variables declared in your c++ code; say double parC <-- paramC ?
      thanx
      ferar

       
      • Ellers

        Ellers - 2005-10-11

        C++ 101: use ostringstream.
        see for example: http://tinyurl.com/7wlao

        or if you're not using STL, then you can use the C stdlib functions atoi and atof, though depending who you talk to, use of those functions is discouraged.

         
      • Ellers

        Ellers - 2005-10-11

        A postscript:

        (a) The more portable string-to-num conversion functions in C are the strto* family, like strtod and strtol.

        (b) TiXmlElement provides this methods:
        QueryIntAttribute()
        QueryDoubleAttribute()

         

Log in to post a comment.