SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
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.
Procedure (Principles)
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:
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:
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 variable for 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.
Example
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.
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.
Notes