如何使用QtSoap解析动态响应?
我目前有以下类型的响应:如何使用QtSoap解析动态响应?,qt,Qt,我目前有以下类型的响应: <?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap
<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><StartBuisnessResponse xmlns=\"http://test.com/kerosene/mytest/\"><StartBuisnessResult><Commodity><_price>45</_price></Commodity><Commodity><_price>36</_price></Commodity></StartBuisnessResult></StartBuisnessResponse></soap:Body></soap:Envelope>
4536
在这里,节点是动态的。在这种情况下,我无法找到使用QtSoap解析响应soapxml的方法
这是用于获取第一种商品的代码:
QString str("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><StartBuisnessResponse xmlns=\"http://cg.nic.in/kerosene/finotest/\"><StartBuisnessResult><Commodity><_price>45</_price></Commodity><Commodity><_price>36</_price></Commodity></StartBuisnessResult></StartBuisnessResponse></soap:Body></soap:Envelope>");
QByteArray *arr = new QByteArray();
arr->append(str);
QtSoapMessage *testMsg = new QtSoapMessage();
testMsg->setContent(*arr);
const QtSoapType &testCont = testMsg->returnValue();
const QtSoapType &price = testCont["Commodity"];
qDebug() << "The value of the _price here is " << price["_price"].value().toString();
QString str(“4536”);
QByteArray*arr=新的QByteArray();
arr->append(str);
QtSoapMessage*testMsg=新的QtSoapMessage();
testMsg->setContent(*arr);
const QtSoapType&testCont=testMsg->returnValue();
const QtSoapType&price=testCont[“商品”];
qDebug()如果您遵循Qt SolutionsQtSoap
中为Google提供的示例,您应该已经开始使用它了
如果您不想尝试,另一种选择是使用QXmlStreamReader:
下面是一些快速代码,可以从中获取\u price
信息:
// add "QT += xml" to your .pro
#include <QXmlStreamReader>
#include <QDebug>
QXmlStreamReader xml(str);
while (!xml.atEnd())
{
if (xml.readNextStartElement())
qDebug() << qPrintable(xml.name().toString());
if(xml.name().toString() == "_price")
{
qDebug() << "\t" << xml.readElementText().toInt();
}
}
//将“QT+=xml”添加到.pro
#包括
#包括
qxmlstreamreaderxml(str);
而(!xml.atEnd())
{
if(xml.readNextStartElement())
qDebug()感谢您的回复,我确实浏览了google示例。在本例中,情况并不完全相同。由于第一个标记之后的后续标记看起来相同,QtSoapType始终返回第一个值。在本例中,我们似乎无法进一步遍历下一个标记。对吗?除了testCont[“name”],还有testCont[index],如下所示:因此您可以传入整数,直到找到所需的元素,而不只是按名称进行映射查找。