bool ParseContext::handleStartElement(QXmlStreamReader &r) { const QStringRef name = r.name(); const Element e = element(name); if (e == VariableElement) { m_currentVariableName = r.readElementText(); return false; } if (!ParseContext::isValueElement(e)) return false; const QXmlStreamAttributes attributes = r.attributes(); const QString key = attributes.hasAttribute(keyAttribute) ? attributes.value(keyAttribute).toString() : QString(); switch (e) { case SimpleValueElement: // This reads away the end element, so, handle end element right here. m_valueStack.push_back(ParseValueStackEntry(readSimpleValue(r, attributes), key)); return handleEndElement(name); case ListValueElement: m_valueStack.push_back(ParseValueStackEntry(QVariant::List, key)); break; case MapValueElement: m_valueStack.push_back(ParseValueStackEntry(QVariant::Map, key)); break; default: break; } return false; }
bool XMLParser::parse() { const char *node; int type; if( !m_pReader ) return false; m_errors = false; while( (type = xml_ReaderNextNode( m_pReader, &node )) > 0 ) { if( m_errors ) return false; switch( type ) { case XML_READER_STARTELEM: { // Read the attributes AttrList_t attributes; const char *name, *value; while( (name = xml_ReaderNextAttr( m_pReader, &value )) != NULL ) attributes[strdup(name)] = strdup(value); handleBeginElement( node, attributes ); map<const char*, const char*, ltstr> ::iterator it = attributes.begin(); while( it != attributes.end() ) { free( (char *)it->first ); free( (char *)it->second ); ++it; } break; } // End element case XML_READER_ENDELEM: { handleEndElement( node ); break; } } } return (type == 0 && !m_errors ); }
void MScoreTextToMXML::write(Xml& xml) { //qDebug("MScoreTextToMXML::write()"); QXmlStreamReader r(text); bool firstTime = true; // write additional attributes only the first time characters are written while (!r.atEnd()) { // do processing r.readNext(); if(r.isCharacters()) { //qDebug("old %s", qPrintable(charFormat2QString(oldFormat))); //qDebug("new %s", qPrintable(charFormat2QString(newFormat))); QString formatAttr = updateFormat(); //qDebug("old %s", qPrintable(charFormat2QString(oldFormat))); //qDebug("new %s", qPrintable(charFormat2QString(newFormat))); //qDebug("Characters '%s'", qPrintable(r.text().toString())); xml.tag(tagname + (firstTime ? attribs : "") + formatAttr, r.text().toString()); firstTime = false; } else if(r.isEndElement()) { //qDebug("EndElem '%s'", qPrintable(r.name().toString())); handleEndElement(r); } else if(r.isStartElement()) { /* qDebug("StartElem '%s'", qPrintable(r.name().toString())); if (r.name() == "font") qDebug(" face='%s' size='%s'", qPrintable(r.attributes().value("face").toString()), qPrintable(r.attributes().value("size").toString())); */ handleStartElement(r); } } if (r.hasError()) { // do error handling qDebug("Error %s", qPrintable(r.errorString())); } }
bool ParseContext::handleStartElement(QXmlStreamReader &r) { const QStringRef name = r.name(); const Element e = element(name); if (e == VariableElement) { m_currentVariableName = r.readElementText(); return false; } if (!ParseContext::isValueElement(e)) return false; const QXmlStreamAttributes attributes = r.attributes(); const QString key = attributes.hasAttribute(keyAttribute) ? attributes.value(keyAttribute).toString() : QString(); switch (e) { case SimpleValueElement: { // This reads away the end element, so, handle end element right here. const QVariant v = readSimpleValue(r, attributes); if (!v.isValid()) { qWarning() << ParseContext::formatWarning(r, QString::fromLatin1("Failed to read element \"%1\".").arg(name.toString())); return false; } m_valueStack.push_back(ParseValueStackEntry(v, key)); return handleEndElement(name); } case ListValueElement: m_valueStack.push_back(ParseValueStackEntry(QVariant::List, key)); break; case MapValueElement: m_valueStack.push_back(ParseValueStackEntry(QVariant::Map, key)); break; default: break; } return false; }
bool XMLParser::parse() { if( !m_pReader ) { return false; } m_errors = false; int ret = xmlTextReaderRead( m_pReader ); while (ret == 1) { if( m_errors ) { return false; } // Get the node type int type = xmlTextReaderNodeType( m_pReader ); switch (type ) { // Error case -1: return false; break; // Begin element case 1: { // Read the element name const xmlChar *eltName = xmlTextReaderConstName( m_pReader ); if( !eltName ) { return false; } // Read the attributes AttrList_t attributes; while( xmlTextReaderMoveToNextAttribute( m_pReader ) == 1 ) { const xmlChar *name = xmlTextReaderConstName( m_pReader ); const xmlChar *value = xmlTextReaderConstValue( m_pReader ); if( !name || !value ) { return false; } attributes[(const char*)name] = (const char*)value; } handleBeginElement( (const char*)eltName, attributes); break; } // End element case 15: // Read the element name const xmlChar *eltName = xmlTextReaderConstName( m_pReader ); if( !eltName ) { return false; } handleEndElement( (const char*)eltName ); break; } ret = xmlTextReaderRead( m_pReader ); } return (ret == 0 && !m_errors ); }