如何使用QtSoap解析动态响应?

时间:2013-04-23 02:29:50

标签: 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: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>

这里,节点是动态的。在这种情况下,我无法找到使用QtSoap解析响应SOAP XML的方法。

这是用于获取第一种商品的代码:

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();

但是在这种情况下如何遍历后续节点?有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果您按照Qt解决方案QtSoap上显示的示例显示他们拥有Google,那么您应该随身携带它。

http://doc.qt.digia.com/solutions/4/qtsoap/index.html

http://doc.qt.digia.com/solutions/4/qtsoap/google-example.html

如果您不想尝试,可以使用QXmlStreamReader:

http://qt-project.org/doc/qt-4.8/qxmlstreamreader.html#details

以下是一些从此处获取_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 Processing

希望有所帮助。