使用C ++从XML文件返回char *以外的数据类型的错误 [英] Errors with returning datatypes other than char* from XML-file using C++
问题描述
这是我第一次使用XML,我目前正在尝试使用C ++从XML文件中返回一个整数(实际上是想返回一个双精度数,但距离还没有那么远).我正在使用RAPIDXML和以下实现:
It's my first time using XML and I am currently trying to return an integer (actually want to return a double but haven't got that far yet) from an XML-file using C++. I'm using RAPIDXML and the following implementation:
所有文件都在同一目录中.
All files are in the same directory.
XML(firstxml.xml):
XML (firstxml.xml):
<?xml version="1.0"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="firstxsd.xsd">
<A>10</A>
<B>Hello</B>
</test>
XML模式(firstxsd.xsd):
XML-Schema (firstxsd.xsd):
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="test">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:integer" name="A"/>
<xs:element type="xs:string" name="B"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
C ++(test.cxx):
C++ (test.cxx):
#include <iostream>
#include <sstream>
#include <fstream>
#include "rapidxml-1.13/rapidxml.hpp"
#include "rapidxml-1.13/rapidxml_print.hpp"
#include <string>
#include <stdio.h>
#include <vector>
int main(int argc, char* argv[])
{
std::ifstream file ("firstxml.xml");
if (file.is_open())
{
file.seekg(0,std::ios::end);
int size = file.tellg();
file.seekg(0,std::ios::beg);
char* buffer = new char [size];
file.read (buffer, size);
file.close();
rapidxml::xml_document<> doc;
doc.parse<0>(buffer);
rapidxml::xml_node<> *node = doc.first_node()->first_node();
//Line which results in error
std::cout << node->value()*10 << std::endl;
delete[] buffer;
}
}
错误:
test.cxx:52:31: error: invalid operands of types ‘char*’ and ‘int’ to binary ‘operator*’
从我在线阅读的教程中,我相信我正在正确地构建文件,因此从节点A解析到C ++文件中的值应该是一个整数.我注意到的一件事是,在RAPIDXML手册中value()的规范如下:
From the tutorials I have read online, I believe I am constructing the files correctly and so the value parsed into the C++ file from the node A should be an integer. One thing I did notice is that in the RAPIDXML manual the specification of value() is as follows:
Ch* value() const;
Description: Gets value of node. Interpretation of value depends on type of node. Note that value will not be zero-terminated if rapidxml::parse_no_string_terminators option was selected during parse. Use value_size() function to determine length of the value.
Returns: Value of node, or empty string if node has no value.
因此,函数定义说它总是返回一个字符指针,但是值的解释取决于节点的类型"这一行意味着它返回了一个与类型有关的值.
So the function definition says it always returns a character pointer, but the line "Interpretation of value depends on type of node" implies it returns a type-dependant value.
感谢您抽出宝贵的时间来查看我的问题,非常感谢您的帮助,
Thank you for taking the time to look at my issue, any help is greatly appreciated,
保罗.
推荐答案
很难说值的解释取决于节点的类型"这句话是什么意思.如果函数返回Ch pointer
,则如果库开发人员提供了此类功能,它将始终返回此类指针或导致运行时异常.自然地,当您尝试将Ch*
与int相乘时,会得到编译时错误.
It is hard to say what does the phrase "Interpretation of value depends on type of node" mean. If the function returns Ch pointer
it will always return such pointer or cause a runtime exception, if library developers provided such feature. Naturally, when you try to multiply a Ch*
by an int you get compile time error.
函数可以返回取决于调用上下文的类型的唯一方法是使用某些特殊的用户定义的返回类型(VARIANT
或类似的东西).在我看来,Ch
只是库的基本字符类型(char
或wchar_t
)的假名.
The only way a function can return a type which depends on call context is to use some special user-defined return type (VARIANT
or something like it). It seems to me that Ch
is just a pseudonim for the basic character type of the library (char
or maybe wchar_t
).
因此,通过解释...",通知开发人员可能意味着用户必须根据节点的类型(进而取决于用户定义的模式)对字符串进行正确的解释.
So by that "interpretation..." notice developers probably meant that the user must make a proper interpretation of the character string in accordance to the type of node (which in turn depends on the user-defined schema).
要将数字的C字符串表示形式转换为相应的类型,可以使用标准C库中的函数,例如atoi()
(转换为int),atof()
(转换为double)等等.
To convert a C-string representation of a number to corresponding types you may use functions from the standard C library, such as atoi()
(converts to int), atof()
(to double) and so on.
这篇关于使用C ++从XML文件返回char *以外的数据类型的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!