【问题标题】:Reading embedded HTML in an XML document with QXmlStreamReader使用 QXmlStreamReader 读取 XML 文档中的嵌入 HTML
【发布时间】:2015-05-24 10:57:21
【问题描述】:

使用QXmlStreamReader,我想要使用HTML 标记的富文本格式的XML 文件。例如,在这个文件中,如果能够访问<em> 和其他用于格式化文本的 HTML 标记,那就太好了。 (并且使用 Qt,我可以将 HTML 放在任何地方,在 QLabel 或其他地方。)

<?xml version="1.0" encoding="UTF-8"?>
<course name="Introductory Course">
    <course-description>Welcome to the <em>basic course</em>.</course-description>
</course>

如果我在&lt;course-description&gt; 的开始元素处使用QXmlStreamReader::readElementText(QXmlStreamReader::IncludeChildElements),我会在&lt;course-description&gt; 中删除标签,例如Welcome to the basic course.

当然,我想这样做,而不必考虑代码中的每个 HTML 标记。

【问题讨论】:

    标签: html c++ xml qt qxmlstreamreader


    【解决方案1】:

    我最终做的是创建一种方法,我可以在我本来会调用QXmlStreamReader::readElementText 的地方使用它。在 XML 文件中,我用 XHTML 命名空间标记了一个标签:

    <?xml version="1.0" encoding="UTF-8"?>
    <course name="Introductory Course">
        <course-description xmlns="http://www.w3.org/1999/xhtml">Welcome to the <em>basic course</em>.</course-description>
    </course>
    

    然后,每当我读取带有QXmlStreamReader 的标签时,我就可以调用readHtml。如果元素具有 XHTML 名称空间,它会读取并返回所有元素,直到到达结束元素。 (这意味着与包含名称空间的元素(上面的&lt;course-description&gt;)同名的元素不能包含在 HTML 代码中。)

    QString MyClass::readHtml(QXmlStreamReader &xml)
    {
        if( xml.namespaceUri().toString() != "http://www.w3.org/1999/xhtml" )
        {
            return xml.readElementText(QXmlStreamReader::IncludeChildElements);
        }
        QString terminatingElement = xml.name().toString();
        QString html;
        QXmlStreamWriter writer(&html);
        do
        {
              xml.readNext();
              switch( xml.tokenType() )
              {
              case QXmlStreamReader::StartElement:
                  writer.writeStartElement(xml.name().toString());
                  writer.writeAttributes(xml.attributes());
                  break;
              case QXmlStreamReader::EndElement:
                  writer.writeEndElement();
                  break;
              case QXmlStreamReader::Characters:
                  writer.writeCharacters(xml.text().toString());
                  break;
              // a more thorough approach would handle these; enumerating them removes a compiler warning
              case QXmlStreamReader::NoToken:
              case QXmlStreamReader::Invalid:
              case QXmlStreamReader::StartDocument:
              case QXmlStreamReader::EndDocument:
              case QXmlStreamReader::Comment:
              case QXmlStreamReader::DTD:
              case QXmlStreamReader::EntityReference:
              case QXmlStreamReader::ProcessingInstruction:
                  break;
              }
        }
        while (!xml.atEnd() && xml.name() != terminatingElement );
        return html;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多