ARTICLE
sXML Library, Token-Based Parsing
In token-based parsing, the parser iterates across all nodes (tokens) in
the tree structure of the XML data, one after the other. By
default, the iterator passes across all subnode branches until the final
node. The parser pauses after every iteration step on a node whose
properties are available in the attributes of the XML reader. If
the parser pauses on the node for an opened element, the list of the
XML attributes there can be accessed.
Procedure (Principles)
An XML reader is created using the factory method CREATE
of the class in question and by passing the source xml to the
XML data, for example:
DATA(reader) = cl_sxml_string_reader= create( xml ).
The static type of the reference variables is then the interface
IF_SXML_READER and its methods and
attributes can be addressed directly.
In the simplest parsing case, the method NEXT_NODE is applied as
many times as it takes to reach the end of the XML :
reader- next_node( ).
Once the method is called, the attributes of the reader with the
required properties of the node can be accessed directly. If the parser
pauses on the node of an opened element, the method
NEXT_ATTRIBUTE
can be used to iterate across the list of attributes of an XML
element:
reader- next_attribute( ).
After this method, the attributes of the reader object contain the name
and the value of the current XML attribute. The end of the data
is displayed by the value of the constants IF_SXML_NODE=>CO_NT_FINAL
in the attribute NODE_TYPE . Any
exceptions should be caught and
handled in a TRY control structure.
Example
See Token-Based Parsing .
Methods and Attributes for Token-Based Parsing
Methods for token-based parsing
Alongside the simple iteration across nodes and attributes, the
interfaces IF_SXML_READER offers
several other methods for token-based parsing, summarized as follows:
Methods for parsing across nodes
NEXT_NODE - Moves the parser to the next node
CURRENT_NODE - Moves the parser back to the node for the opened
element of the current node, if available. This method is ignored by
value nodes or closed elements.
PUSH_NODE - Moves the parser back to the node for the opened
element of the direct parent node.
SKIP_NODE - Parses all nodes from a node for an opened element to
the associated closed element. The parser then pauses on the closed
element. If required, the current node and all subnodes are passed to an
XML writer. This method is ignored by value nodes or
closed elements.
Methods for reading the attribute list
NEXT_ATTRIBUTE - Reads the next attribute in the list
NEXT_ATTRIBUTE_VALUE - Sets the attribute VALUE or
VALUE_RAW of the read explicitly to the value of the current
attribute
GET_ATTRIBUTE_VALUE - Sets the attribute VALUE of the
reader to the value of a specific attribute
Notes
Token-based parsing parsing is designed mainly for forwards interaction
through the XML data. Free navigation, as possible in
iXML Library in DOM , is not
recommended here. The method PUSH_BACK makes it possible to move
back a step, but does not restore the reader to the state it had when it
reached the node using NEXT_NODE . After a PUSH_BACK , the
parsed node does not always produced the same result as the last time.
In particular, subnodes already parsed could be skipped.
If an XML element has several attributes with the same name, the
system uses all of these attributes - in the order in which they appear
(see Example ).
The method CURRENT_NODE is most useful when reading an attribute
list, so jump back to the start.
Apart from simply skipping nodes, the method SKIP_NODE can also
be used to check the subtrees or the full tree for errors or to copy
trees. In particular, the format of the copied tree or subtree can be
transformed into a different format (see
Example ).
Examples
See Steps in Token-Based Parsing .
See Methods for Token-Based Parsing
.
Attributes for token-based parsing
The attributes of the reader with the properties of the current node
are:
NODE_TYPE - Node type in accordance with the constants of the
interface IF_SXML_NODE
PREFIX - Namespace prefix
NAME - Element name
NSURI - Namespace
VALUE_TYPE - Type of the value in accordance with the constants
of the interface IF_SXML_VALUE :
CO_VT_TEXT - Text data in the attribute VALUE
CO_VT_TEXT - Raw data in the attribute VALUE_RAW
VALUE - Character-like value (if text data)
VALUE_RAW - Byte-like value (if raw data)
The value of the constants IF_SXML_NODE= CO_NT_FINAL in the
attribute NODE_TYPE indicate that the end of the XML data
was reached.
Notes
When parsing, the attributes of a reader are only overwritten by
non-initial content. If the parser is set to a literal element without a
name, for example, the preceding content of the attribute NAME is
kept. This applies particularly to the attribute VALUE , which is
not initialized if a node does not have a value.
The attributes are declared in the interface IF_SXML_READER . In a
reader class, they can also be addressed using alias names.
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved