Преглед изворни кода

hello,world working. Forgot how hard XML parsing can be.

Lee Thomason пре 14 година
родитељ
комит
85403d8935
3 измењених фајлова са 25 додато и 7 уклоњено
  1. 22 5
      tinyxml2.cpp
  2. 3 2
      tinyxml2.h
  3. BIN
      tinyxml2.suo

+ 22 - 5
tinyxml2.cpp

@@ -73,6 +73,13 @@ XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
 
 
 void XMLNode::Print( FILE* fp, int depth )
+{
+	for( XMLNode* node = firstChild; node; node=node->next ) {
+		node->Print( fp, depth );
+	}
+}
+
+void XMLNode::PrintSpace( FILE* fp, int depth ) 
 {
 	for( int i=0; i<depth; ++i ) {
 		fprintf( fp, "    " );
@@ -138,6 +145,7 @@ char* XMLComment::ParseDeep( char* p )
 XMLDocument::XMLDocument() : 
 	charBuffer( 0 )
 {
+	root = new XMLNode( this );
 }
 
 
@@ -153,8 +161,11 @@ bool XMLDocument::Parse( const char* p )
 {
 	charBuffer = CharBuffer::Construct( p );
 	XMLNode* node = 0;
+	
 	char* q = Identify( charBuffer->mem, &node );
+	root->InsertEndChild( node );
 	node->ParseDeep( q );
+
 	return true;
 }
 
@@ -184,13 +195,19 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
 	// - Everthing else is unknown to tinyxml.
 	//
 
-	const char* xmlHeader = { "<?xml" };
-	const char* commentHeader = { "<!--" };
-	const char* dtdHeader = { "<!" };
-	const char* cdataHeader = { "<![CDATA[" };
+	static const char* xmlHeader = { "<?xml" };
+	static const char* commentHeader = { "<!--" };
+	static const char* dtdHeader = { "<!" };
+	static const char* cdataHeader = { "<![CDATA[" };
+
+	static const int xmlHeaderLen = 5;
+	static const int commentHeaderLen = 4;
+	static const int dtdHeaderLen = 2;
+	static const int cdataHeaderLen = 9;
 
-	if ( XMLNode::StringEqual( p, xmlHeader, 5 ) ) {
+	if ( XMLNode::StringEqual( p, commentHeader, commentHeaderLen ) ) {
 		returnNode = new XMLComment( this );
+		p += commentHeaderLen;
 	}
 	else {
 		TIXMLASSERT( 0 );

+ 3 - 2
tinyxml2.h

@@ -48,7 +48,7 @@ class XMLNode
 public:
 
 	XMLNode* InsertEndChild( XMLNode* addThis );
-	void Print( FILE* cfile, int depth );			// prints leading spaces.
+	virtual void Print( FILE* cfile, int depth );
 
 protected:
 	XMLNode( XMLDocument* );
@@ -90,6 +90,7 @@ protected:
 	XMLNode*		next;
 
 private:
+	void PrintSpace( FILE* cfile, int depth );			// prints leading spaces.
 
 };
 
@@ -100,7 +101,7 @@ public:
 	XMLComment( XMLDocument* doc );
 	virtual ~XMLComment();
 
-	void Print( FILE* cfile, int depth );
+	virtual void Print( FILE* cfile, int depth );
 
 protected:
 	char* ParseDeep( char* );