Page 1 of 1

XMLsample use for ?

PostPosted: Thu Aug 22, 2013 11:00 pm
by samsam598

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.


Re: XMLsample use for ?

PostPosted: Fri Aug 23, 2013 1:37 am
by jerome
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:
Code: Select all
enum MyTag
class MyParser : XMLParser
   MyTag tag;
   void ProcessCharacterData(char * data)
         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 =
            MyTag curTag = (MyTag);
               tag = curTag;
               tag = none;

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 :)



Re: XMLsample use for ?

PostPosted: Fri Aug 23, 2013 1:45 am
by jerome
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.


Re: XMLsample use for ?

PostPosted: Fri Aug 23, 2013 7:06 am
by samsam598
Hi Jerome,

It was a big loss to me that I overlooked that thread[url](viewtopic.php?f=1&t=208&p=857&hilit=xml#p849)[/url].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.