Greetings,
Is there any example code that reading from a xml file to the console and writing back?
Thanks for the help in advance.
Regards,
Sam
import "ecere"
import "XMLParser"
define app = (XMLReadApp)__thisModule.application;
class ConsoleParser : XMLParser
{
void ProcessKeyword(char * keyWord)
{
// keyWord first holds the tag name
if(closingTag)
// The parser will come here for closing tags and for self-closing tags (e.g. <tag /> or <?xml ?>)
Print(openingTag ? "<" : "</", keyWord);
else
// Otherwise the parser will come here for an opening tag
Print("<", keyWord);
// Note: the current tag depth is in the xmlDepth data member
// This loops through any tag attribute
while(GetWord())
{
// After invoking GetWord(), keyWord now holds the attribute name
// This prints a space before the attribute name
if(strcmp(keyWord, "?")) Print(" ");
// This prints the attribute name...
Print(keyWord);
// After invoking GetWord(), keyWord will now hold the attribute value which we'll print within quotes
if(GetWord())
Print("=\"", keyWord, "\"");
}
Print(">");
}
void ProcessCharacterData(char * data)
{
// The parser will come here for data (stuff not inside tags)
// This will include characters (including spaces) both within inner and outer tags
// Further processing could be done here...
Print(data);
}
}
class XMLReadApp : Application
{
void Main()
{
if(argc > 1)
{
File f = FileOpen(argv[1], read);
if(f)
{
ConsoleParser parser { };
uint size = f.GetSize();
byte * buffer = new byte[size];
f.Read(buffer, 1, size);
// For XML files that are UTF-16 encoded with a BOM, this will handle the little endian UTF-16
if(buffer[0] == 0xFF && buffer[1] == 0xFE)
{
char * newBuffer = UTF16toUTF8(buffer+2);
delete buffer;
buffer = newBuffer;
size = strlen(newBuffer);
}
parser.Parse(buffer, size);
delete f;
delete parser;
}
}
system("pause");
}
}
Users browsing this forum: No registered users and 2 guests