Qt5 xml转Json

前端之家收集整理的这篇文章主要介绍了Qt5 xml转Json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

由于业务需要,把xml数据转化为 json数据。

这个例子 展示了 Qt的QDom解析xml 以及 QJson的用法

xml

<?xml version="1.0" encoding="utf-8"?>

<root> 
  <mission title="有序列表中,Type属性可能的赋值是:" mid="11">
    <answer result="0" aid="42">disc</answer>
    <answer result="0" aid="44">circle</answer>
    <answer result="1" aid="45">a</answer>
    <answer result="0" aid="43">square</answer>
  </mission>
  <mission title="以下符号中,输出一根水平线条的标签是:" mid="2">
    <answer result="0" aid="8"><line/></answer>
    <answer result="1" aid="6"><hr/></answer>
    <answer result="0" aid="7"><img/></answer>
    <answer result="0" aid="5"><br/></answer> 
  </mission>  
  <mission title="HTML的基本结构是:" mid="3">
    <answer result="0" aid="10"><root><head></head><body></body></root></answer>
    <answer result="0" aid="11"><html><title></title><body></body></html></answer>
    <answer result="1" aid="9"><html><head></head><body></body></html></answer>
    <answer result="0" aid="12"><root><title></title><body></body></root></answer> 
  </mission>  
  <mission title="用来给网页增加背景图片属性是:" mid="7">
    <answer result="0" aid="27">src</answer>
    <answer result="0" aid="26">bgcolor</answer>
    <answer result="0" aid="28">image</answer>
    <answer result="1" aid="29">background</answer> 
  </mission>  
  <mission title="超链接标签是:" mid="8">
    <answer result="0" aid="31"><font></font></answer>
    <answer result="0" aid="33"><em></em></answer>
    <answer result="1" aid="30"><a></a></answer>
    <answer result="0" aid="32"><link></link></answer>
  </mission>
  <code>1</code>
  <mess>成功发送答题信息</mess> 
</root>

C++代码
  QString str=reply->readAll();
    QDomDocument doc;
    doc.setContent(str);
    QDomElement elem=doc.documentElement();
    QDomNodeList Missions=elem.elementsByTagName("mission");
    QJsonArray JsonArray;
    QDomNodeList tmpNodes;

    for(int i=0;i<=Missions.length();i++)
    {
       tmpNodes=Missions.at(i).toElement().elementsByTagName("answer");
       QJsonObject tmpAnserJson;
       QJsonArray tmparray;
       for(int k=0;k<tmparray.count();k++)tmparray.removeAt(k);
       tmpAnserJson.insert("title",Missions.at(i).toElement().attributeNode("title").value());
        tmpAnserJson.insert("mid",Missions.at(i).toElement().attributeNode("mid").value());
        for(int j=0;j<tmpNodes.length();j++)
        {
            QJsonObject tmpjsonObj;
            tmpjsonObj.insert("answer",tmpNodes.at(j).toElement().text());
            tmpjsonObj.insert("result",tmpNodes.at(j).toElement().attributeNode("result").value());
            tmpjsonObj.insert("aid",tmpNodes.at(j).toElement().attributeNode("aid").value());
            tmparray.append(tmpjsonObj);
        }
        tmpAnserJson.insert("detail",tmparray);
        JsonArray.append(tmpAnserJson);
    }
    QJsonDocument jsondoc;
    jsondoc.setArray(JsonArray);
    QString strjson=jsondoc.toJson();
    qDebug()<<strjson;
    qDebug()<<str;

json:
[
    {
        "detail": [
            {
                "aid": "45","answer": "a","result": "1"
            },{
                "aid": "42","answer": "disc","result": "0"
            },{
                "aid": "44","answer": "circle",{
                "aid": "43","answer": "square","result": "0"
            }
        ],"mid": "11","title": "有序列表中,Type属性可能的赋值是:"
    },{
        "detail": [
            {
                "aid": "33","answer": "<em></em>",{
                "aid": "30","answer": "<a></a>",{
                "aid": "31","answer": "<font></font>",{
                "aid": "32","answer": "<link></link>","mid": "8","title": "超链接标签是:"
    },{
        "detail": [
            {
                "aid": "17","answer": "<p></p>",{
                "aid": "13","answer": "<hr/>",{
                "aid": "16",{
                "aid": "14","answer": "<h3></h3>",{
                "aid": "15","answer": "<t></t>","mid": "4","title": "下列哪一个属于标题标签:"
    },{
        "detail": [
            {
                "aid": "38","answer": "<ol></ol>",{
                "aid": "40","answer": "<dl></dl>",{
                "aid": "41","answer": "<list></list>",{
                "aid": "39","answer": "<ul></ul>","mid": "10","title": "有序列表的标签为:"
    },{
        "detail": [
            {
                "aid": "4","answer": "<huanhang/>",{
                "aid": "1","answer": "<br/>",{
                "aid": "2",{
                "aid": "3","answer": "<img/>","mid": "1","title": "以下符号中,强制换行标签是:"
    },{
        "detail": [],"mid": "","title": ""
    }
]
需要注意几点:

一。关于XML

xml获取根结点:(元素)

doc.documentElement();
结点,分文本结点,属性结点..

而元素是一种特别的结点,有文本和属性

QDomNode 结点转元素 需要

.toElement()

获取某一元素下面的元素,通过TagName(多个)
 QDomNodeList Missions=elem.elementsByTagName("mission");
获取元素的某个属性值:
.attributeNode("title").value()
二.关于Json

Json对象是:

 QJsonObject
在对象上插入属性(可以插入多个,多个属性
 tmpjsonObj.insert("answer",tmpNodes.at(j).toElement().text());
值可以是对象,也可以是JsonArray,也可以是字符串。

Json数组是

QJsonArray tmparray;
数组 不是 键/值方式存储,直接插入即可。取出直接索引.
tmparray.append(tmpjsonObj);

作者原创,请勿转载。

猜你在找的XML相关文章