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 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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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
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
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.
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()