void TiXmlElementA::StreamOut( TIXMLA_OSTREAM * stream ) const { (*stream) << "<" << value; TiXmlAttributeA* attrib; for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() ) { (*stream) << " "; attrib->StreamOut( stream ); } // If this node has children, give it a closing tag. Else // make it an empty tag. TiXmlNodeA* node; if ( firstChild ) { (*stream) << ">"; for ( node = firstChild; node; node=node->NextSibling() ) { node->StreamOut( stream ); } (*stream) << "</" << value << ">"; } else { (*stream) << " />"; } }
int TiXmlElementA::QueryDoubleAttribute( const char* name, double* dval ) const { TiXmlAttributeA* node = attributeSet.Find( name ); if ( !node ) return TIXMLA_NO_ATTRIBUTE; return node->QueryDoubleValue( dval ); }
int TiXmlElementA::QueryIntAttribute( const char* name, int* ival ) const { TiXmlAttributeA* node = attributeSet.Find( name ); if ( !node ) return TIXMLA_NO_ATTRIBUTE; return node->QueryIntValue( ival ); }
const char * TiXmlElementA::Attribute( const char * name ) const { TiXmlAttributeA* node = attributeSet.Find( name ); if ( node ) return node->Value(); return 0; }
const char* TiXmlDeclarationA::Parse( const char* p, TiXmlParsingDataA* data ) { p = SkipWhiteSpace( p ); // Find the beginning, find the end, and look for // the stuff in-between. TiXmlDocumentA* document = GetDocument(); if ( !p || !*p || !StringEqual( p, "<?xml", true ) ) { if ( document ) document->SetError( TIXMLA_ERROR_PARSING_DECLARATION, 0, 0 ); return 0; } // TiXmlParsingDataA data( p, prevData ); if ( data ) { data->Stamp( p ); location = data->Cursor(); } p += 5; version = ""; encoding = ""; standalone = ""; while ( p && *p ) { if ( *p == '>' ) { ++p; return p; } p = SkipWhiteSpace( p ); if ( StringEqual( p, "version", true ) ) { TiXmlAttributeA attrib; p = attrib.Parse( p, data ); version = attrib.Value(); } else if ( StringEqual( p, "encoding", true ) ) { TiXmlAttributeA attrib; p = attrib.Parse( p, data ); encoding = attrib.Value(); } else if ( StringEqual( p, "standalone", true ) ) { TiXmlAttributeA attrib; p = attrib.Parse( p, data ); standalone = attrib.Value(); } else { // Read over whatever it is. while( p && *p && *p != '>' && !isspace( *p ) ) ++p; } } return 0; }
void TiXmlElementA::Print( FILE* cfile, int depth ) const { int i; for ( i=0; i<depth; i++ ) { fprintf( cfile, " " ); } fprintf( cfile, "<%s", value.c_str() ); TiXmlAttributeA* attrib; for ( attrib = attributeSet.First(); attrib; attrib = attrib->Next() ) { fprintf( cfile, " " ); attrib->Print( cfile, depth ); } // There are 3 different formatting approaches: // 1) An element without children is printed as a <foo /> node // 2) An element with only a text child is printed as <foo> text </foo> // 3) An element with children is printed on multiple lines. TiXmlNodeA* node; if ( !firstChild ) { fprintf( cfile, " />" ); } else if ( firstChild == lastChild && firstChild->ToText() ) { fprintf( cfile, ">" ); firstChild->Print( cfile, depth + 1 ); fprintf( cfile, "</%s>", value.c_str() ); } else { fprintf( cfile, ">" ); for ( node = firstChild; node; node=node->NextSibling() ) { if ( !node->ToText() ) { fprintf( cfile, "\n" ); } node->Print( cfile, depth+1 ); } fprintf( cfile, "\n" ); for( i=0; i<depth; ++i ) fprintf( cfile, " " ); fprintf( cfile, "</%s>", value.c_str() ); } }
void TiXmlElementA::SetAttribute( const char * name, const char * _value ) { TiXmlAttributeA* node = attributeSet.Find( name ); if ( node ) { node->SetValue( _value ); return; } TiXmlAttributeA* attrib = new TiXmlAttributeA( name, _value ); if ( attrib ) { attributeSet.Add( attrib ); } else { TiXmlDocumentA* document = GetDocument(); if ( document ) document->SetError( TIXMLA_ERROR_OUT_OF_MEMORY, 0, 0 ); } }
TiXmlNodeA* TiXmlElementA::Clone() const { TiXmlElementA* clone = new TiXmlElementA( Value() ); if ( !clone ) return 0; CopyToClone( clone ); // Clone the attributes, then clone the children. for(TiXmlAttributeA* attribute = attributeSet.First(); attribute; attribute = attribute->Next() ) { clone->SetAttribute( attribute->Name(), attribute->Value() ); } for ( TiXmlNodeA* node = firstChild; node; node = node->NextSibling() ) { clone->LinkEndChild( node->Clone() ); } return clone; }
const char* TiXmlElementA::Parse( const char* p, TiXmlParsingDataA* data ) { p = SkipWhiteSpace( p ); TiXmlDocumentA* document = GetDocument(); if ( !p || !*p ) { if ( document ) document->SetError( TIXMLA_ERROR_PARSING_ELEMENT, 0, 0 ); return 0; } // TiXmlParsingDataA data( p, prevData ); if ( data ) { data->Stamp( p ); location = data->Cursor(); } if ( *p != '<' ) { if ( document ) document->SetError( TIXMLA_ERROR_PARSING_ELEMENT, p, data ); return 0; } p = SkipWhiteSpace( p+1 ); // Read the name. const char* pErr = p; p = ReadName( p, &value ); if ( !p || !*p ) { if ( document ) document->SetError( TIXMLA_ERROR_FAILED_TO_READ_ELEMENT_NAME, pErr, data ); return 0; } TIXMLA_STRING endTag ("</"); endTag += value; endTag += ">"; // Check for and read attributes. Also look for an empty // tag or an end tag. while ( p && *p ) { pErr = p; p = SkipWhiteSpace( p ); if ( !p || !*p ) { if ( document ) document->SetError( TIXMLA_ERROR_READING_ATTRIBUTES, pErr, data ); return 0; } if ( *p == '/' ) { ++p; // Empty tag. if ( *p != '>' ) { if ( document ) document->SetError( TIXMLA_ERROR_PARSING_EMPTY, p, data ); return 0; } return (p+1); } else if ( *p == '>' ) { // Done with attributes (if there were any.) // Read the value -- which can include other // elements -- read the end tag, and return. ++p; p = ReadValue( p, data ); // Note this is an Element method, and will set the error if one happens. if ( !p || !*p ) return 0; // We should find the end tag now if ( StringEqual( p, endTag.c_str(), false ) ) { p += endTag.length(); return p; } else { if ( document ) document->SetError( TIXMLA_ERROR_READING_END_TAG, p, data ); return 0; } } else { // Try to read an attribute: TiXmlAttributeA* attrib = new TiXmlAttributeA(); if ( !attrib ) { if ( document ) document->SetError( TIXMLA_ERROR_OUT_OF_MEMORY, pErr, data ); return 0; } attrib->SetDocument( document ); const char* pErr = p; p = attrib->Parse( p, data ); if ( !p || !*p ) { if ( document ) document->SetError( TIXMLA_ERROR_PARSING_ELEMENT, pErr, data ); delete attrib; return 0; } // Handle the strange case of double attributes: TiXmlAttributeA* node = attributeSet.Find( attrib->Name() ); if ( node ) { node->SetValue( attrib->Value() ); delete attrib; return 0; } attributeSet.Add( attrib ); } } return p; }