Greetings!
I compiled and tried to run the net\XMLsample but nothing happened after it is running.What's the sample used for ,could you please briefing?What I tried to do is in eC to read and write XML files from outside resources.How to do in eC?Appreciated.
Yes I knew once you've recommended me to use Json other than XML.But as you know there are existing XML files you have to read their content and or write back after modifictions.This is why I once again asked for the interaction with XML in eC.
Regards,
Sam
XMLsample use for ?
Re: XMLsample use for ?
Hi Sam,
That sample does not do anything by itself.
It is just demonstrating how you can use the XMLParser module from the extras to parse a stream of data.
The base XMLParser class has 2 virtual methods you can override in your parser:
virtual void ProcessKeyword(char * keyWord)
This is for tags themselves, as well as values within the tag itself. The string of the tag will be available in the passed 'keyWord' parameter.
If 'openingTag' is set to true, this is the opening tag, e.g. <SomeTag>
If 'closingTag' is set to true, this is the closing tag, e.g. </SomeTag>
If both openingTag and closingTag are set to true, it's a tag that is both the opening and ending, i.e. there won't be a closing tag for it, e.g. <SomeTag/>
To read the values within the tag itself, you can follow that XMLSample which has all the values within the tags, e.g.:
<Object id="SOMEID" type=5>
You first compare the tag, then you can call GetWord() to read the next attribute string, and GetWord() again if a parameter for that attribute string is expected.
(keyWord will originally be "Object", after the first GetWord() it will become "id", and the next GetWord() will be "SOMEID", then next one "type", and next one "5").
virtual void ProcessCharacterData(char * data)
This method is for processing character data, i.e. the data that is not itself within a tag, e.g. <SomeTag>Some text in between the tag</SomeTag>
The data is simply the string of all characters within a tag.
Depending on the complexity and layout of the XML schema your are trying to parse, you may use various approaches for remembering the various states.
One simple approach if you have a very flat layout is to use an enumeration type for each possible tag, and keep a 'MyTag tag' member for the parser which is set in ProcessKeyword() whenever 'openingTag' is set to true. Here's an example of how this can be automated:
A simplistic level only works with a rather flat hierarchy, you'd need some kind of stacks for deep recursion and tags which can occur at multiple levels
You could also build a more generic higher level XML parser, working off a schema, or an eC class defining the schema, by building a class inheriting from XMLParser (Please let me know if you do and you want to share it with the community)
To invoke your parser, you simply call Parse() with a buffer and the number of bytes available in it. The XMLParser will support streaming, so you can use it directly with a Socket object or with a buffered file read, calling it with additional data as it otherwise becomes available.
If you're looking for something more powerful, take a look at expat .
It's a decent XML parser written in C used all over the place, you could write eC bindings for it, though I'm not familiar enough with it to tell you how much easier to use or more powerful it would be than the eC extras/XMLParser class
Regards,
Jerome
That sample does not do anything by itself.
It is just demonstrating how you can use the XMLParser module from the extras to parse a stream of data.
The base XMLParser class has 2 virtual methods you can override in your parser:
virtual void ProcessKeyword(char * keyWord)
This is for tags themselves, as well as values within the tag itself. The string of the tag will be available in the passed 'keyWord' parameter.
If 'openingTag' is set to true, this is the opening tag, e.g. <SomeTag>
If 'closingTag' is set to true, this is the closing tag, e.g. </SomeTag>
If both openingTag and closingTag are set to true, it's a tag that is both the opening and ending, i.e. there won't be a closing tag for it, e.g. <SomeTag/>
To read the values within the tag itself, you can follow that XMLSample which has all the values within the tags, e.g.:
<Object id="SOMEID" type=5>
You first compare the tag, then you can call GetWord() to read the next attribute string, and GetWord() again if a parameter for that attribute string is expected.
(keyWord will originally be "Object", after the first GetWord() it will become "id", and the next GetWord() will be "SOMEID", then next one "type", and next one "5").
virtual void ProcessCharacterData(char * data)
This method is for processing character data, i.e. the data that is not itself within a tag, e.g. <SomeTag>Some text in between the tag</SomeTag>
The data is simply the string of all characters within a tag.
Depending on the complexity and layout of the XML schema your are trying to parse, you may use various approaches for remembering the various states.
One simple approach if you have a very flat layout is to use an enumeration type for each possible tag, and keep a 'MyTag tag' member for the parser which is set in ProcessKeyword() whenever 'openingTag' is set to true. Here's an example of how this can be automated:
Code: Select all
enum MyTag
{
none,
myTag1,
myTag2,
myTag3
};
class MyParser : XMLParser
{
MyTag tag;
void ProcessCharacterData(char * data)
{
switch(tag)
{
case myTag1: PrintLn("myTag1: ", data); break;
case myTag2: PrintLn("myTag2: ", data); break;
case myTag3: PrintLn("myTag3: ", data); break;
}
}
void ProcessKeyword(char * keyWord)
{
NamedLink nl;
EnumClassData tagData = class(MyTag).data;
for(nl = tagData.values.first; nl; nl = nl.next)
{
if(!strcmpi(keyWord, nl.name))
{
MyTag curTag = (MyTag)nl.data;
if(openingTag)
tag = curTag;
else
tag = none;
break;
}
}
}
}
You could also build a more generic higher level XML parser, working off a schema, or an eC class defining the schema, by building a class inheriting from XMLParser (Please let me know if you do and you want to share it with the community)
To invoke your parser, you simply call Parse() with a buffer and the number of bytes available in it. The XMLParser will support streaming, so you can use it directly with a Socket object or with a buffered file read, calling it with additional data as it otherwise becomes available.
If you're looking for something more powerful, take a look at expat .
It's a decent XML parser written in C used all over the place, you could write eC bindings for it, though I'm not familiar enough with it to tell you how much easier to use or more powerful it would be than the eC extras/XMLParser class
Regards,
Jerome
Re: XMLsample use for ?
Also take a look at that other thread from last year where I answered you the first time, I had explained most of this and I had written a sample for you to output the XML on the console.
-Jerome
-Jerome
Re: XMLsample use for ?
Hi Jerome,
It was a big loss to me that I overlooked that thread(http://www.ecere.com/forums/viewtopic. ... =xml#p849).It worked perfect!I love it,forget expat at the moment!
I think with the two thread I can dive into your xml parser and study it.One more thing,based on the sample you've provide on that thread,it would be grateful if you would like to add a couple of lines on demonstrate how to write back to XML file after modification on some contents.
Appreciated again for the guidance.
Regards,
Sam
It was a big loss to me that I overlooked that thread(http://www.ecere.com/forums/viewtopic. ... =xml#p849).It worked perfect!I love it,forget expat at the moment!
I think with the two thread I can dive into your xml parser and study it.One more thing,based on the sample you've provide on that thread,it would be grateful if you would like to add a couple of lines on demonstrate how to write back to XML file after modification on some contents.
Appreciated again for the guidance.
Regards,
Sam