XMLNode* XMLDecoderUTF8::parseElement()
	{
		String name;
		advance(1);
		/* spec says no whitespaces allowd - whatever */
		skipWhitespace();
		if(!parseName( name))
			throw CVTException("Malformed element name");

		/* START Tag */
		XMLElement* element = new XMLElement( name );
		while( _rlen ) {
			skipWhitespace();
			if( match( "/>" ) ) {
				advance( 2 );
				return element;
			} else if( match( '>' ) )	{
				advance( 1 );
				break;
			} else {
				element->addChild( parseAttribute() );
			}
		}

		/* Content */
		 skipWhitespace();
		 while( !match("</") ) {
			if( match("<?") )
				element->addChild( parsePI() );
			else if( match("<!--") )
				element->addChild( parseComment() );
			else if( match("<") )
				element->addChild( parseElement() );
			else if( match("<![CDATA[") )
				element->addChild( parseCData() );
			else
				element->addChild( parseText() );
			skipWhitespace();
		}

		/* END Tag */
		advance( 2 );
		/* spec says no whitespaces allowd - whatever */
		skipWhitespace();
		String ename;
		if( ! parseName( ename ) )
			throw CVTException("Malformed element name");
		if( name != ename )
			throw CVTException("Names in start- and end-tag differ");
		skipWhitespace();
		if( !match('>') )
			throw CVTException("Missing '>'");
		advance( 1 );
		return element;
	}
Exemple #2
0
XmlNode * XmlReader::parseElement () {
    //XmlNode * e = NULL;
    char c = skipSpaces ();
    if (c == '\0') { // end of data
        return NULL;
    } else if (c == '<') { // we have a XML fragment
        c = getNextChar ();
        if (c == '?') {
            if (parseXmlHeader ()) {
                return parseElement ();
            }
            return NULL;
        } else if (c == '!') {
            if (startsWith (m_buffer, "![CDATA[", m_pos)) { // pure XML CDATA Definition
                m_pos+=8; // skip header ![CDATA[
                int end = indexOf (m_buffer, "]]>", m_pos);
                if (end == -1) {
                    MESSAGE ("Error: Non terminated CDATA section.\n");
                    m_failure = true;
                    return NULL; // CDATA not terminated
                }
                char * data = strdup (m_buffer, m_pos, m_pos+end);
                if (m_iconv) {
                    char * orig = data;
                    data = toUTF8 (orig, end);
                    free (orig);
                }
                m_pos += end+3;  // avoid trailing ]]>
                return new XmlNode (data, CDATA);
            }
            if (parseSpecial ('-', '-', "-->")) { // ignore comments <!-- -->
                return parseElement ();
            } else if (parseSpecial ('D', 'O', ">")) { // ignore external document type declaration <!DOCTYPE >
                // TODO: support parsing (and ignoring) internal doctype declaration
                return parseElement ();
            }
            MESSAGE ("Error: Unsupported tag syntax\n");
            m_failure = true;
            return NULL;
        }
        return parseTag (c);
    }
    return parseCData ();
}