XML - Document handler functionΒΆ

Document handler function

!function func_id(event_code, arg1, arg2, arg3, arg4)

This is the most important of the event handlers. A document handler function must be defined, in order to retrieve information from the XML data. The arguments sent to the function vary according to the event code.

The document handler function is set with xml_set_document_handler(). See Registration of handler functions. The event codes are defined in xml.h.

XML_EV_CHARACTERS

arg1 = string; arg2 = length

Reports a chunk of character data in the string.

XML_EV_END_DOCUMENT

Received at the end of the document.

XML_EV_END_ELEMENT

arg1 = name

End-tag found.

XML_EV_IGNORABLE_WHITESPACE

arg1 = string; arg2 = length

Reports a chunk of ignorable white space.

XML_EV_PROCESSING_INSTRUCTION

arg1 = target; arg2 = data

Called for each processing instruction found.

XML_EV_SET_DOCUMENT_LOCATOR

arg1 = pub_id; arg2 = sys_id; arg3 = line; arg4 = column

Called for each document event, specifying the location.

XML_EV_START_DOCUMENT

Received at the beginning of the document. May be preceded by an XML_EV_SET_DOCUMENT_LOCATOR event.

XML_EV_START_ELEMENT

arg1 = name

Received at the start of a tag.The following functions can be called from within the document handler function to access specific attributes:

xml_attr_get_length()

Returns the number of attributes.

xml_attr_get_name(position)

Returns the name of the attribute at the position specified.

xml_attr_get_type(position | name)

Returns the data type of the attribute whose position or name is specified.

xml_attr_get_value(position | name)

Returns the value of the attribute whose position or name is specified.

xml_attr_read(record_id)

Read the attributes into the specified record structure (defined by !record). The function returns the number of attributes.

EXAMPLE

This example counts different properties in the XML file:

!temp elem_cnt,,i4     /* Number of elements */
!temp attr_cnt,,i4     /* Number of attributes */
!temp char_cnt,,i4     /* Number of characters */
!temp spc_cnt,,i4      /* Number of ignorable white space characters */

     xml_set_document_handler(DocHandlerFunc)

!function DocHandlerFunc(ev, arg1, arg2, arg3, arg4) {
     switch (ev) {
          case = XML_EV_START_ELEMENT:
               elem_cnt = elem_cnt + 1
               attr_cnt = attr_cnt + xml_attr_get_length()
               break
          case = XML_EV_START_DOCUMENT:
               break
          case = XML_EV_SET_DOCUMENT_LOCATOR:
               break
          case = XML_EV_PROCESSING_INSTRUCTION:
               break
          case = XML_EV_IGNORABLE WHITESPACE
               spc_cnt = spc_cnt + arg2
               break
          case = XML_EV_END_ELEMENT:
               break
          case = XML_EV_END_DOCUMENT:
               break
          case = XML_EV_CHARACTERS:
               char_cnt = char_cnt + arg2
               break
     }
}

RELATED TOPICS

XML