SAXState-class              package:XML              R Documentation

_A _v_i_r_t_u_a_l _b_a_s_e _c_l_a_s_s _d_e_f_i_n_i_n_g _m_e_t_h_o_d_s _f_o_r _S_A_X _p_a_r_s_i_n_g

_D_e_s_c_r_i_p_t_i_o_n:

     This is a degenerate virtual class which others are expected to
     sub-class  when they want to use S4 methods as handler functions
     for SAX-based XML parsing. The idea is that one can pass both i) 
     a collection of handlers to 'xmlEventParse' which are simply  the
     generic functions for the different SAX actions, and ii) a
     suitable object to maintain state across the different SAX calls.
     This is used to perform the method dispatching to get the
     appropriate behavior for the action. Each of these methods is
     expected to return the updated state object and the SAX parser
     will pass this in the next callback.

     We define this class here so that we can provide default methods
     for each of the different handler actions. This allows other
     programmers to define new classes to maintain state that are
     sub-class of 'SAXState' and then they do not have to implement
     methods for each of the different handlers.

_O_b_j_e_c_t_s _f_r_o_m _t_h_e _C_l_a_s_s:

     A virtual Class: No objects may be created from it.

_M_e_t_h_o_d_s:

     _c_o_m_m_e_n_t._S_A_X 'signature(content = "ANY", .state = "SAXState")': ... 

     _e_n_d_E_l_e_m_e_n_t._S_A_X 'signature(name = "ANY", .state = "SAXState")': ... 

     _e_n_t_i_t_y_D_e_c_l_a_r_a_t_i_o_n._S_A_X 'signature(name = "ANY", base = "ANY", sysId
          = "ANY", publicId = "ANY", notationName = "ANY", .state =
          "SAXState")': ... 

     _p_r_o_c_e_s_s_i_n_g_I_n_s_t_r_u_c_t_i_o_n._S_A_X 'signature(target = "ANY", content =
          "ANY", .state = "SAXState")': ... 

     _s_t_a_r_t_E_l_e_m_e_n_t._S_A_X 'signature(name = "ANY", atts = "ANY", .state =
          "SAXState")': ... 

     _t_e_x_t._S_A_X 'signature(content = "ANY", .state = "SAXState")': ... 

_A_u_t_h_o_r(_s):

     Duncan Temple Lang

_R_e_f_e_r_e_n_c_e_s:

     <URL: http://www.w3.org/XML>, <URL: http://www.xmlsoft.org>

_S_e_e _A_l_s_o:

     'xmlEventParse'

_E_x_a_m_p_l_e_s:

     # For each element in the document, grab the node name
     # and increment the count in an vector for this name.

     # We define an S4 class named ElementNameCounter which
     # holds the vector of frequency counts for the node names.

      setClass("ElementNameCounter",
                  representation(elements = "integer"), contains = "SAXState")

     # Define a method for handling the opening/start of any XML node
     # in the SAX streams.

      setMethod("startElement.SAX",  c(.state = "ElementNameCounter"),
                function(name, atts, .state = NULL) {

                  if(name %in% names(.state@elements))
                      .state@elements[name] = as.integer(.state@elements[name] + 1)
                  else
                      .state@elements[name] = as.integer(1)
                  .state
                })

      filename = system.file("exampleData", "eurofxref-hist.xml.gz", package = "XML")

     # Parse the file, arranging to have our startElement.SAX method invoked.
      z = xmlEventParse(filename, genericSAXHandlers(), state = new("ElementNameCounter"), addContext = FALSE)

      z@elements

       # Get the contents of all the comments in a character vector.

      setClass("MySAXState",
                  representation(comments = "character"), contains = "SAXState")

      setMethod("comment.SAX",  c(.state = "MySAXState"),
                function(content, .state = NULL) {
                  cat("comment.SAX called for MySAXState\n")
                  .state@comments <- c(.state@comments, content)
                  .state
                })

      filename = system.file("exampleData", "charts.svg", package = "XML")
      st = new("MySAXState")
      z = xmlEventParse(filename, genericSAXHandlers(useDotNames = TRUE), state = st)
      z@comments

