ABAP Keyword Documentation → ABAP - Reference → Data Interfaces and Communication Interfaces → ABAP and XML → Class Libraries for XML → sXML Library → sXML Library - Parse
sXML Library - Object-Oriented Parsing
Just as in token-based parsing, object-oriented parsing creates an XML reader and uses its methods to iterate across the XML data. Some of the methods are different, however. The methods used for object-oriented parsing wrap methods for token-based parsing and provide object-oriented access to the current node. Instead of read the attributes of the reader directly after a parser step, the methods used for object-oriented parsing return references to objects that represent the current node. The methods and attributes of the classes and interfaces of these objects an be used to access the properties of the node. Unlike in token-based parsing, only the relevant values are available.
7.31 | 7.40 | 7.54
An XML reader is created using the factory method CREATE of the class in question (as in
token-based parsing) and by passing the source
xml to the XML data, for example:
DATA(reader) = cl_sxml_string_reader=>create( xml ).
In the simplest parsing case, the method READ_NEXT_NODE is applied as many times as it takes to reach the end of the XML:
DATA(node) = reader->read_next_node( ).
The static type of the reference variable
node is then IF_SXML_NODE,
which points to a node object. The return value is initial at the end of the XML data. The attribute
TYPE of the node object displays the type of the node in accordance with the constants CO_NT_... of
the interface IF_SXML_NODE. This means that a down cast can be performed to a more specific reference
node, which makes it possible to access the properties of the
current node. If the parser is located on the node of an opened element, the node object has the class
CL_SXML_OPEN_ELEMENT with the interface IF_SXML_OPEN_ELEMENT.
The methods of this interface enables access to the XML attributes of the element. For example, GET_ATTRIBUTES can be used to pass references to attribute objects for all attributes to an internal table.
Any exceptions should be caught and handled in a
TRY control structure.
Methods for Object-Oriented Parsing
The following methods of the interface IF_SXML_READER are designed specially for object-oriented parsing. They return a reference with the static type IF_SXML_NODE, which points to a node object and can be cast to its more concrete object type.
- READ_NEXT_NODE - Like NEXT_NODE but with the return value NODE.
- READ_CURRENT_NODE - Like CURRENT_NODE but with the return value NODE.
If access to the current node is required after PUSH_BACK, the node object can be fetched using READ_CURRENT_NODE.
These methods are all that is needed for all simple parsing tasks. For accessing XML attributes, a node object of an opened element contains the appropriate methods. To move the parser back to a preceding node or to parse the current node with its subnodes, the methods PUSH_BACK and SKIP_NODE can also be used here. Further token-based parsing methods should not usually be used at the same time.
- Token-based parsing and object-oriented parsing are not strictly separate from each other. Methods such as NEXT_NODE and READ_NEXT_NODE can be used alternately in the same program. The attributes of the reader can be used to access the properties of a node in object-oriented parsing too. This is not recommended, though, since the program must remain easy to read.
- If an XML element has several attributes with the same name, then, unlike with token-based parsing, only one of these attributes is used during object-oriented parsing. This attribute has the value of the last attribute with the same name (see Example).
- A node object returned in object-oriented parsing can be passed directly to an object-oriented XML writer.
- Object-oriented parsing is more user-friendly than token-based parsing, but the many extra objects that are created can affect performance.