This post was last edited by Summer on 2019-1-16 09:31
1. What is the technical problem to be solved in this article?
Solve the problem in the prior art that when parsing XML documents in C++ language, the base class tags derived class data that appear in the original XML document can only parse the base class data according to the base class type according to the conventional parsing method, but cannot completely parse the derived class data.
2. Technical background
In prior art, use boost::serialization to transfer derived classes with a base class pointer or use BOOST_CLASS_EXPORT macro.
Both of the above schemes in existing technologies are based on the abstract class mechanism, but since abstract classes cannot be instantiated as concrete objects that can be used, objects declared as base classes in XML documents cannot be parsed. The present invention is intended to ensure that the data of both the base class object itself and the derived class object marked by the base class object can be correctly parsed and completely preserved.
3. The technical scheme provided in this article
This paper does not use the abstract class mechanism, but introduces the base class pointer into the class called by the base class, declares the base class parsing function as a virtual function, and calls the parsing function through the base class pointer in the parsing process, so as to apply polymorphism to dynamically parse and store the data, and finally ensures that the data of both the base class object and the derived class object derived from the base class can be stored in the base class defined pointer. Specific steps: Step 1: C++ code to design all classes that can correspond to the data tags in the XML document to be parsed, and parse XML data one by one from the top-level tags. Step 2: For each tag parsed, determine whether there are other tags that inherit the tag as a base class, that is, whether the tag type derives other tag types; Step 3: If it is determined that the current tag is not inherited as a base class, parse the current tag according to the normal parsing steps. Step 4: If it is determined that the current tag is inherited as a base class, further determine whether the content described by the base class tag is subordinate to the base class object or a derived class object; Step 5: If the content of the current base class tag is determined to describe the base class object, parse the current tag according to the normal steps. Step 6: If the current base class tag content describes the derived class object, modify the code to parse according to the new parsing method: modify the data structure of the previous type that defines the base class object and define the base class pointer instead; Modify the base class parsing function and declare it to be a virtual function; Step 7: Where the base class object was originally defined for parsing, the base class pointer is defined instead, and the specific type of memory is allocated to it according to the type described by the label content, and the base class pointer is used to call the parsing function to parse the current label. Step 8: Parse and play the current tag to determine whether there are still tags to be parsed; Step 9: If it is determined that there are no tags to be parsed, it means that the parsing has been completed and the data has been saved to the class with the highest label of the corresponding XML document in the C++ class. Step 10: If it is determined that there are still tags that have not been resolved, parse the next tag according to step 2.
Flow chart attached
4. Summary
C++ introduces a base class pointer to assign objects of the corresponding type to the XML tag content according to whether it is a base class or a derived class, and uses the defined base class pointer to call a parsing function declared as an imaginary function to parse the XML base class tag content. In this way, when the XML base class tag describes the base class object data, the base class parsing function will be dynamically called according to the polymorphism of C++, and the base class data described by the base class tag can be correctly parsed and preserved. When the XML base class tag describes the derived class object data, the derived class parsing function will be dynamically called according to the polymorphism of C++, and the derived class data described by the base class tag can also be correctly parsed and preserved.
|