Re: [Vala] [Genie] Using MarkupParser



Nicolas wrote:
Hi Patrick,

You can use the example from Caleb72:

[indent=3]
uses
  Xml

class JWMReader : Object
  path : string

  init
     Parser.init()

  final
     Parser.cleanup()

  def run()
     var doc = Parser.parse_file(path)
     var node = doc->get_root_element()
     parse_node(node)

  def set_source(pathname : string)
     path = pathname

  def private parse_node(node : Xml.Node*)
     if node is null
        return
     case node->type
        when ElementType.ELEMENT_NODE
           print "NODE: %s", node->name
           if node->properties is not null
              get_attributes(node, node->properties)
           if node->children is null
              node = node->next
           else
              node = node->children
        when ElementType.TEXT_NODE
           if not (node->content.strip() is "")
              print "TEXT: %s", node->content.strip()
           if node->next is null
              node = node->parent->next
           else
              node = node->next
        when ElementType.COMMENT_NODE
           node = node->next
        default
           print("DO NOT RECOGNISE NODE!")
           return        parse_node(node)

  def private get_attributes(node : Xml.Node*, properties : Xml.Attr*)
     if properties is null
        return
print "ATTR: %s VALUE: %s", properties->name, node->get_prop(properties->name)
     properties = properties->next
     get_attributes(node, properties)

init
  var reader = new JWMReader()
  reader.set_source("/root/.jwmrc-tray")
  reader.run()

See you,
Nicolas.



Thanks Nicholas - actually I am Caleb72.

That small piece of code was the result of about a day's work trying to understand that library - looking both at the Vala documentation and the original C library reference material. Then it was down to trial and error.

By the way - do you think the concerns I raised on the Puppy forum are correct regarding this code? I'm not actually doing anything to cleanup the Xml.Doc*, Xml.Node* and Xml.Attr* pointers I create and my reading of Parser.cleanup() was that everything is not necessarily destroyed. Genie has delete which I could use - not sure if I should be using something defined in the libxml library though.

My understanding of Genie thus far is that when you specifically define pointers as I have in this case, you take the responsibility for management of the memory, the reference counter isn't applicable.

Regards
Patrick



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]