Rich text label parsing method and device based on IOS platform, equipment and medium
By preprocessing and transcribing special characters of rich text data, defining parent node tags, and then using the libxml system library for parsing, the problems of incomplete data and fragmented tag text when parsing rich text on the iOS platform are solved, ensuring that the development progress is not affected.
Patent Information
- Application Number
- CN202310166975.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-17
- Publication Date
- 2025-12-16
- Estimated Expiration
- 2043-02-17
AI Technical Summary
When parsing rich text on the iOS platform, it cannot recognize special tags and special characters in the text, resulting in incomplete parsing data, fragmented tag text, and delays in development.
By preprocessing rich text data, translating special characters, defining parent node tags, and using the libxml system library to traverse and parse the optimized rich text data, complete parsed data is generated.
This solution resolves issues such as incomplete data and fragmented tag text when parsing rich text on the iOS platform, ensuring that development progress is not affected.
Smart Images

Figure CN116070619B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of natural language processing of artificial intelligence voice semantic recognition, and particularly relates to a rich text label parsing method and device based on an IOS platform, equipment and a medium. BACKGROUND
[0002] In the development process of a project or a webpage, the content of rich text needs to be edited and processed. Currently, the common practice in the industry is to parse the nodes of rich text data, that is, to convert the rich text data into the NSData type (NSData is the objectified encapsulation of a byte buffer), create an instance according to the data, find the nodes and store them in an array, and then loop to find the child nodes under the node. For example, TFHpple and OCGumbo, these open source libraries and their implementation methods are very convenient for developers to quickly parse rich text labels.
[0003] The above method is not a problem for some commonly used labels and ordinary text, but when it comes to some special label symbols (such as "&;", "<;", or ">", etc.) or special characters (such as "<") in the text, IOS cannot judge and identify when parsing rich text because it calls the system library libxml (a C language library for processing XML that can easily cross multiple platforms) during parsing. Especially in the latter case, if the developer continues to implement in this way, it is easy to encounter some unexpected exceptions, such as incomplete parsed data or the text in the label being cut off, which may block the development and delay the development progress. SUMMARY
[0004] The present application provides a rich text label parsing method and device based on an IOS platform, equipment and a medium, aiming to solve the problem that IOS cannot judge and identify when calling the system library libxml to parse rich text, which may cause incomplete parsed data, cut-off label text, and other problems, and delay the development progress.
[0005] To solve the above technical problems, in a first aspect, the present application provides a rich text label parsing method based on an IOS platform, comprising:
[0006] Obtaining rich text data and preprocessing the rich text data to generate preprocessed rich text data;
[0007] Optimizing the preprocessed rich text data according to a preset optimization rule to generate optimized rich text data;
[0008] Defining a parent node label and calling a preset system library to traverse and parse the optimized rich text data with the parent node label to generate parsed rich text data.
[0009] Preferably, the step of preprocessing the rich text data to generate preprocessed rich text data comprises:
[0010] determining whether the rich text data is from a preset source;
[0011] if yes, normalizing the rich text data to generate normalized text data;
[0012] if no, determining whether the rich text data contains a translated character;
[0013] when the rich text data contains the translated character, translating the translated character into a normal character to generate translated rich text data;
[0014] using the normalized text data or the translated rich text data as the preprocessed rich text data.
[0015] Preferably, the step of optimizing the preprocessed rich text data according to a preset optimization rule comprises:
[0016] extracting text in the preprocessed rich text data within a preset string identifier group based on NSScanner to generate a string, wherein the preset string identifier group comprises a start identifier and an end identifier, and the last character of the string is the end identifier;
[0017] determining whether the string contains the start identifier;
[0018] when the string contains the start identifier, grouping the string according to the start identifier as a division point to generate a plurality of groups of first sub-strings and a group of second sub-strings, wherein the end identifier is contained in the last end of the second sub-strings;
[0019] determining whether the second sub-strings are of a label type;
[0020] when the second sub-strings are of the label type, converting the remaining start identifiers in the string except for the first start identifier into a preset replacement identifier;
[0021] when the second sub-strings are not of the label type, converting all the start identifiers in the string into a preset replacement identifier.
[0022] Preferably, the step of defining a parent node label and calling a preset system library to traverse and parse the optimized rich text data according to the parent node label to generate parsed rich text data comprises:
[0023] defining a parent node label, nesting the optimized rich text data in the parent node label to generate nested rich text data;
[0024] calling libxml system library and traversing and parsing the nested rich text data according to the parent node tag to generate parsed rich text data.
[0025] Preferably, the step of traversing and parsing the nested rich text data according to the parent node tag to generate parsed rich text data comprises:
[0026] performing node searching on the nested rich text data to obtain all child node texts in the nested rich text data;
[0027] defining all the child node texts to generate defined child node texts;
[0028] objectifying the defined child node texts according to a preset format to generate parsed rich text data.
[0029] Preferably, the step of objectifying the defined child node texts according to a preset format comprises:
[0030] creating an xmlDocPtr object document according to the defined child node texts, initializing the xmlDocPtr object document according to NSData by calling an htmlReadMemory method to generate an initialized object document, wherein xmlDocPtr is a pointer for parsed document, the NSData is a function for converting an html string into a data type in iOS, and the htmlReadMemory is an open source method provided in a libxml library in an iOS system and used for extracting a structure body containing xml information from the NSData;
[0031] judging whether the xmlDocPtr object in the initialized object document is empty;
[0032] returning nil when the xmlDocPtr object in the initialized object document is empty;
[0033] creating a context pointer of xmlXpathContextPt type according to the initialized object document when the xmlDocPtr object in the initialized object document is not empty, wherein the xmlXpathContextPt is a pointer for defining an Xpath context, and the Xpath language is an XML language path and is a language for determining the position of a part in an XML document;
[0034] judging whether the initialized object document is empty;
[0035] returning nil when the initialized object document is empty;
[0036] Otherwise, query according to the query word in the added parent node and the context pointer, generate an xmlXpathObjectPtr object XpathObj, wherein the xmlXpathObjectPtr is an object pointer used to define an Xpath;
[0037] Judge whether the object XpathObj is empty or not;
[0038] When the object XpathObj is empty, return and release the context pointer;
[0039] Otherwise, create a node set pointer xmlNodeSetPtr object nodes according to the nodesetval of the object XpathObj, wherein the node set contains an element number nodeNr and a node data nodeTab array, and xmlNodeSetPtr is a pointer pointing to the node object nodes;
[0040] Judge whether the object nodes is empty or not;
[0041] When the object nodes is empty, return and release the object XpathObj and the context;
[0042] When the object nodes is not empty, create a variable array NSMutableArray object resultNodes;
[0043] According to the object resultNodes, traverse the node number nodeNr in the object nodes;
[0044] Attribute analysis is performed on the object xmlModePtr in the corresponding node data nodeTab array to generate an analysis array;
[0045] Combine the nodeName node name, nodeContent content, attributeName attribute and raw label contained rich text parameters in the analysis array into a node dictionary resultForNode, and add it to the object resultNodes to generate a resultNodes array;
[0046] Object assignment is performed on the dictionary data in the resultNodes array to generate an object array.
[0047] Preferably, before the step of object assignment on the dictionary data in the resultNodes array, it comprises:
[0048] The alternative identification contained in the dictionary data is replaced with the beginning identification, and the resultNodes array is updated.
[0049] In a second aspect, the present application further provides a rich text label parsing device based on an IOS platform, comprising:
[0050] A rich text data acquisition module is configured to acquire rich text data, and pre-process the rich text data to generate pre-processed rich text data.
[0051] An optimization module is configured to perform optimization processing on the pre-processed rich text data according to a preset optimization rule to generate optimized rich text data.
[0052] An analyzed rich text data module is configured to define a parent node label, and call a preset system library to perform traversal analysis on the optimized rich text data according to the parent node label to generate analyzed rich text data.
[0053] In a third aspect, the present application further provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the rich text label parsing method based on the IOS platform according to any one of the above aspects.
[0054] In a fourth aspect, the present application further provides a computer readable storage medium having a computer program stored thereon, wherein the computer program is executed by a processor to implement the steps of the rich text label parsing method based on the IOS platform according to any one of the above aspects.
[0055] The rich text label parsing method, device, equipment and medium based on the IOS platform according to the present application acquire rich text data, pre-process the rich text data to generate pre-processed rich text data, perform optimization processing on the pre-processed rich text data according to a preset optimization rule to generate optimized rich text data, and call a preset system library to perform traversal analysis on the optimized rich text data according to the parent node label to generate analyzed rich text data. The special label symbols involved in the rich text can be optimized and translated, and then the traversal is performed according to the translation result, which can solve the problem that the IOS cannot judge and identify when calling the system library libxml to parse the rich text in the prior art, and the problems of incomplete parsed data, cut-off label text and delayed development progress are avoided. BRIEF DESCRIPTION OF DRAWINGS
[0056] Figure 1 FIG. 1 is a flowchart of a rich text label parsing method based on an IOS platform according to an embodiment of the present application;
[0057] Figure 2It is an embodiment of the structure of the IOS platform-based rich text label parsing device schematic diagram;
[0058] Figure 3 It is an embodiment of the structure of the computer device schematic block diagram.
[0059] The implementation of the present application, functional features and advantages will be further described with reference to the accompanying drawings. DETAILED DESCRIPTION
[0060] In order to make the purpose, technical scheme and advantages of the present application more clear, the present application will be further described in detail below in combination with the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application, and are not used to limit the present application.
[0061] Those skilled in the art can understand that, unless specifically stated, the singular form "a", "an" and "the" used herein also includes the plural form. It should be further understood that the phrase "comprising" used in the specification of the present application means that the features, integers, steps, operations, elements, units, units and / or components exist, but do not exclude the existence or addition of one or more other features, integers, steps, operations, elements, units, units, components and / or their combinations. It should be understood that when we say that an element is "connected" or "coupled" to another element, it can be directly connected or coupled to other elements, or there can be intermediate elements. In addition, the "connection" or "coupling" used herein can include wireless connection or wireless coupling. The phrase "and / or" used herein includes all or any unit and all combinations of the associated listed items.
[0062] Those skilled in the art can understand that, unless otherwise defined, all terms (including technical terms and scientific terms) used herein have the same meaning as that generally understood by those skilled in the art to which the present application belongs. It should also be understood that terms such as those defined in a general dictionary should be understood as having a meaning consistent with that in the prior art, and unless specifically defined as here, should not be interpreted in an idealized or overly formal sense.
[0063] Reference Figure 1 It is an embodiment of the present application based on the IOS platform-based rich text label parsing method, comprising:
[0064] S1: obtaining rich text data, and preprocessing the rich text data to generate preprocessed rich text data;
[0065] S2: optimizing the preprocessed rich text data according to the preset optimization rule to generate optimized rich text data;
[0066] S3: defining a parent node label, and calling a preset system library to traverse and parse the optimized rich text data with the parent node label, to generate parsed rich text data.
[0067] As described in step S1, the normal text is a text format without any bold, underline, italic, graphics, symbols or special characters and special printing formats. The normal text only saves the text without saving the format setting. However, in the website development or webpage editing, in order to enrich the text content and optimize the webpage layout, it is inevitable to use bold, underline, italic, graphics, symbols or special characters with special meanings (such as "&;", "<" or ">"). Since iOS parses the rich text by calling the system library libxml, it cannot judge and identify during parsing. If the developer does not process the special characters, some unexpected abnormalities may occur, such as incomplete data obtained by parsing, or the text in the label is cut off, which may block the development and delay the development progress. Based on this scenario, the application proposes a rich text label parsing method based on the IOS platform. First, the obtained rich text data needs to be preprocessed, wherein the preprocessing includes identifying the rich text data based on the rich text data, identifying that the rich text data contains translation characters with special meanings, such as "&;", "<" or ">", and if the rich text data contains special characters, the special characters will be converted into normal characters such as symbol &, < or >, to generate preprocessed rich text data, to prevent the label from not being parsed or the Element attribute content from continuing to contain translation characters.
[0068] For the above step S2, the filtered results in step S1 are further optimized, and the preset scanning plug-in NSScanner (NSScanner is a class for scanning specified characters in a string) is used to scan the string string within the preset specific string identifier, such as the string starting with the symbol “<” and ending with the symbol “>”. In some application scenarios of medical technology, the applicable age of some vaccines such as nine-valent vaccines and the like has different age requirements. In the inoculation condition of a certain XX virus vaccine, inoculating the vaccine before the age of 30 will have a better defense and inhibition effect on the XX virus. Therefore, based on the text data of the inoculation condition of a certain XX virus vaccine, the corresponding text is parsed to determine whether the person meets the inoculation condition in the text description related to the age of the inoculator. For example, the string “he has exceeded 30 years old” is scanned to obtain the string “he has exceeded 30 years old>”, and it is determined whether the scanned string contains <. If it does not contain <, it means that “he has exceeded 30 years old” is a normal string, and the next string can be processed. If it contains <, for example, the string scanned by NSScanner is “age < 30 years old”, the string contains the symbol <, and it is necessary to further confirm whether “<” in the string is a comparison operator or a string identifier. Therefore, it is determined whether “< 30 years old” in “registration age < 30 years old” is a label (i.e., whether “<” is a label symbol), so as to determine whether “<” in “< 30 years old” is a comparison operator or a string identifier, to obtain further optimized optimized rich text data, and to exclude the interference of the comparison symbol < in the text on the calling system library libxml.
[0069] As described in the above step S3, a parent node label is defined for the optimized optimized rich text data, and the system library libxml traverses and parses with the parent node label as the unique parent node, so that a more complete parsed data can be obtained.
[0070] In one embodiment, the step of preprocessing the rich text data to generate preprocessed rich text data includes:
[0071] determining whether the source of the rich text data is a preset source;
[0072] if yes, normalizing the rich text data to generate normalized text data;
[0073] if no, determining whether the rich text data contains a translation character;
[0074] when the rich text data contains the translation character, translating the translation character into a normal character to generate translated rich text data;
[0075] The canonical text data or the translated rich text data is taken as the preprocessed rich text data.
[0076] As mentioned above, the preprocessing step of the rich text data includes judging the source of the rich text data, which includes the rich text data imported from the self database and the rich text data imported from other resources. For the rich text data imported from the self database, the rich text data is normalized in the background editing, i.e. the translated characters such as “&;”, “<;” or “>;” in the rich text data are deleted to generate canonical text data. If the rich text data is imported from other resources, the preprocessing is performed to check whether there are translated characters “&;”, “<;” or “>;”, etc. If yes, the translated characters are converted into normal characters &,
[0077] In one embodiment, the step of optimizing the preprocessed rich text data according to the preset optimization rule includes:
[0078] Based on the NSScanner, the text in the preprocessed rich text data within the preset string identifier group is extracted to generate a string, wherein the preset string identifier group includes a start identifier and an end identifier, and the last character of the string is the end identifier;
[0079] It is judged whether the string contains the start identifier;
[0080] When the string contains the start identifier, the string is grouped by taking the start identifier as a division point to generate a plurality of groups of first sub-strings and a group of second sub-strings, wherein the end identifier is contained in the last end of the second sub-string;
[0081] It is judged whether the second sub-string is of a label type;
[0082] When the second sub-string is of the label type, the remaining start identifiers in the string except the first start identifier are converted into preset alternative identifiers;
[0083] When the second sub-string is not of the label type, all the start identifiers in the string are converted into preset alternative identifiers.
[0084] As described above, for the pre-processed rich text data obtained after pre-processing, the pre-processed rich text data is scanned by using the NSScanner plug-in, and in some embodiments, other scanning plug-ins can also be used for scanning, which is not limited here. Based on the NSScanner, the text in the pre-processed rich text data in the preset string identifier group is extracted to generate a string. For example, if the specific string identifier is “<” at the beginning and the symbol “>”, the content after each character segment “<” can be removed. For example, if the text content is “<This year, the required age for a certain post is <30 years old>”, the “This year, the required age for a certain post is <30 years old>” part is extracted as a string. The extracted string still contains the character “<”, so the character “<” is used as a division point to divide “This year, the required age for a certain post is <30 years old>” into “This year, the required age for a certain post is” and “<30 years old>”. If the extracted string contains N characters “<”, the string should be divided into N+1 sub-strings by taking each “<” as a division point. The first N sub-strings are the first sub-string, i.e. the first sub-string does not contain the symbol “>”, and the N+1 sub-string contains the symbol “>” and is the second sub-string, i.e. the second sub-string is “<30 years old>”. Since the second sub-string “<30 years old>” still contains a character “<”, it is necessary to further determine whether the character “<” is a string identifier or a comparison operator, i.e. to determine whether the second sub-string is a label type. If “<30 years old>” is a label type, the remaining opening identifiers in the entire string except the first opening identifier are converted into a preset alternative identifier, such as replacing “<This year, the required age for a certain post is <30 years old>” with “<This year, the required age for a certain post is <30 years old>”, where the symbol “<” is different from “<”, and “<” only represents a normal text symbol, while “<” represents a comparison operator. If “<30 years old>” is not a label type, all characters “<” in the string “<This year, the required age for a certain post is <30 years old>” are converted into a normal text symbol “<”, and the translated rich text content is “<This year, the required age for a certain post is <30 years old>”. Referring to this method, all strings in the pre-processed rich text data are processed in a loop, so that if the text contains a comparison symbol <, the interference caused by calling the system library libxml for parsing can be excluded.
[0085] Further, the character "<>" and are used here for illustration, and are not limited to the character "<" as the string identifier. In some embodiments, other characters such as "【" can also be used as the string identifier. Similarly, the character "<" can also be translated into "【". The specific character used as the string identifier or the character for translation can be set according to the actual use scenario.
[0086] In one embodiment, the step of defining the parent node tag and calling the preset system library to traverse and parse the optimized rich text data with the parent node tag to generate parsed rich text data includes:
[0087] Defining the parent node tag and nesting the optimized rich text data in the parent node tag to generate nested rich text data;
[0088] Calling the libxml system library and traversing and parsing the nested rich text data with the parent node tag to generate parsed rich text data.
[0089] As described above, a parent node tag is defined for the optimized rich text data, and the rich text html content in the optimized rich text data obtained based on step S2 is nested in the parent node tag. Then, the system library libxml is called to parse the nested rich text data. During parsing, the system library libxml traverses and parses the nested rich text data with the parent node tag as the unique parent node. In this way, relatively complete parsed data can be obtained.
[0090] In one embodiment, the step of traversing and parsing the nested rich text data with the parent node tag to generate parsed rich text data includes:
[0091] Node searching is performed on the nested rich text data to obtain all child node texts in the nested rich text data;
[0092] All the child node texts are defined to generate defined child node texts;
[0093] The defined child node texts are objectified in a preset format to generate parsed rich text data.
[0094] As described above, the nested rich text data is traversed and parsed. In this embodiment, the method of converting NSData into NSArray is used for conversion. However, before conversion, the text content in the nested rich text data needs to be subjected to node searching and child node definition, so as to be traversed and parsed in the subsequent parsing process.
[0095] In one embodiment, the step of objectifying the defined child node texts in a preset format includes:
[0096] According to the definition of the sub-node text, an xmlDocPtr object document is created, according to the NSData, an htmlReadMemory method is called to initialize the xmlDocPtr object document, and an initialized object document is generated, wherein the xmlDocPtr is a pointer for parsing a document, the NSData is a function for converting an html string into a data type in iOS, and the htmlReadMemory is an open source method provided in a libxml library in an iOS system, and is used for extracting a structure body containing xml information from the NSData;
[0097] It is judged whether the xmlDocPtr object in the initialized object document is empty or not;
[0098] When the xmlDocPtr object in the initialized object document is empty, nil is returned;
[0099] When the xmlDocPtr object in the initialized object document is not empty, a context pointer of an xmlXpathContextPt type is created according to the initialized object document, wherein the xmlXpathContextPt is a pointer for defining an Xpath context, and the Xpath language is an XML language path, and is a language for determining a position of a certain part in an XML document;
[0100] It is judged whether the initialized object document is empty or not;
[0101] When the initialized object document is empty, nil is returned;
[0102] Otherwise, according to the query word in the added parent node and the context pointer, an xmlXpathObjectPtr object XpathObj is generated, wherein the xmlXpathObjectPtr is an object pointer for defining an Xpath;
[0103] It is judged whether the object XpathObj is empty or not;
[0104] When the object XpathObj is empty, the context pointer is returned and released;
[0105] Otherwise, according to a pointing structure body nodesetval of the object XpathObj, a node set pointer xmlNodeSetPtr object nodes is created, wherein the node set contains an element number nodeNr and a node data nodeTab array, and the xmlNodeSetPtr is a pointer for pointing to the node object nodes;
[0106] determining whether the object nodes is empty;
[0107] when the object nodes is empty, returning and releasing the objects XpathObj and context;
[0108] when the object nodes is not empty, creating a variable array NSMutableArray object resultNodes;
[0109] traversing the number of nodes nodeNr in the object nodes according to the object resultNodes;
[0110] performing attribute parsing on the object xmlModePtr in the corresponding node data nodeTab array to generate a parsed array;
[0111] combining the nodeName node name, nodeContent content, attributeName attribute, and raw label contained rich text parameters in the parsed array into a node dictionary resultForNode, and adding the node dictionary resultForNode to the object resultNodes to generate the resultNodes array;
[0112] performing object assignment on the dictionary data in the resultNodes array to generate an object array.
[0113] As described above, the main step flow of objectizing the definition subnode is as follows: creating an xmlDocPtr (parsed document pointer) object document, xmlDoc is a structure body that saves xml related information, xmlDocPtr is a structure pointer object; according to NSData (converting an html string into a data type in iOS), calling the htmlReadMemory method for initialization, htmlReadMemory is an open source method provided in the libxml library in the iOS system, which is used to extract the structure body containing xml information from NSData, determining whether the xmlDocPtr object is empty, and returning nil if it is empty; otherwise, the next operation is performed;
[0114] According to the initialization object document, an xmlXpathContextPt (used to define a context pointer of Xpath, i.e. context) type context pointer is created, and it is judged whether it is empty. If yes, nil is returned, otherwise, according to the added parent node query word (query refers to the parent node label defined in step S3, and the format is: / / label, such as / / parent) and the context, an xmlXpathObjectPtr (used to define an object pointer of Xpath, i.e. XpathObj) object XpathObj is generated by querying and calculating (xmlXpathEvalExpression is a function used to query and calculate Xpath expression, and the query and calculation results are stored into the object pointer XpathObj) an xmlXpathEvalExpression method, and it is continuously judged whether XpathObj is empty. If yes, the context is released and returned, otherwise, the next step is continuously operated. Wherein, Xpath language is an XML language path (XML Path language), which is a language used to determine the position of a part in an XML document;
[0115] According to the XpathObj pointing structure body nodesetval (node set), a node set pointer xmlNodeSetPtr object nodes is created, which contains the number of elements in the set nodeNr and a node data nodeTab. It is continuously judged whether nodes is empty. If yes, XpathObj and context are released and returned, otherwise, a variable array NSMutableArray object resultNodes (result node) is created, the node number nodeNr in nodes is traversed, the object xmlModePtr in the corresponding node data nodeTab array is parsed, the nodeName node name, nodeContent content, attributeName attribute, raw label containing rich text and other parameters are combined into a node dictionary resultForNode, and are added to the resultNodes array.
[0116] Finally, according to the obtained resultNodes array, the dictionary data in the array is object assigned (i.e. dictionary objectization), and an object array is regenerated, which can objectize the data and facilitate quick access;
[0117] Further, in the object and dictionary conversion process, when the object attribute is traversed, it is necessary to determine whether the attribute exists to prevent a crash caused by a null pointer, that is, when the object attribute is not null, the object attribute is converted, and if the object attribute is null, the corresponding object attribute is converted to a null value (in a computer program, preventing a null pointer is different from preventing a null value of a pointer, preventing a null pointer means that a return value cannot be found, and the program will report an error, and preventing a null value of a pointer means that the null pointer is not null, but points to a value of "0").
[0118] In one embodiment, before the step of performing object assignment on the dictionary data in the resultNodes array, the method comprises:
[0119] The placeholder in the dictionary data is replaced with the start identifier, and the resultNodes array is updated.
[0120] As described above, the difference between the label symbol "<" and the ordinary text symbol "<" is still obvious, and the symbol "<" has been replaced with the symbol "<" during preprocessing and optimization processing. Therefore, before the step of assigning a value to the element object content attribute based on the resultNodes array, the dictionary data is object assigned to generate an object array, the text symbol "<" can be replaced with the label symbol "<", so that more complete parsed data can be obtained.
[0121] In a second aspect, referring to Figure 2 The application also provides a rich text label parsing device based on an IOS platform, comprising:
[0122] A rich text data acquisition module 100 is configured to acquire rich text data, and preprocess the rich text data to generate preprocessed rich text data.
[0123] An optimization module 200 is configured to perform optimization processing on the preprocessed rich text data according to a preset optimization rule to generate optimized rich text data.
[0124] A parsed rich text data module 300 is configured to define a parent node label, and call a preset system library to perform traversal parsing on the optimized rich text data according to the parent node label to generate parsed rich text data.
[0125] In one embodiment, the rich text data acquisition module 100 comprises:
[0126] A first judgment unit is configured to determine whether the source of the rich text data is a preset source.
[0127] The specification text data generation unit is configured to, if yes, normalize the rich text data to generate normalized text data;
[0128] The second judging unit is configured to, if no, judge whether the rich text data contains a translated character;
[0129] The translated rich text data unit is configured to, when the rich text data contains the translated character, translate the translated character into a normal character to generate translated rich text data;
[0130] The preprocessed rich text data generation unit is configured to take the normalized text data or the translated rich text data as the preprocessed rich text data.
[0131] In one embodiment, the optimization module 200 comprises:
[0132] The string generation unit is configured to extract text in a preset string identifier group from the preprocessed rich text data based on an NSScanner to generate a string, wherein the preset string identifier group comprises a start identifier and an end identifier, and the last character of the string is the end identifier;
[0133] The third judging unit is configured to judge whether the string contains the start identifier;
[0134] The substring generation unit is configured to, when the string contains the start identifier, group the string by taking the start identifier as a division point to generate a plurality of first substrings and a second substring, wherein the end identifier is contained in the last end of the second substring;
[0135] The fourth judging unit is configured to judge whether the second substring is a label type;
[0136] The first conversion unit is configured to, when the second substring is a label type, convert the remaining start identifiers in the string except the first start identifier into a preset replacement identifier;
[0137] The second conversion unit is configured to, when the second substring is not a label type, convert all the start identifiers in the string into a preset replacement identifier.
[0138] In one embodiment, the parsed rich text data module 300 comprises:
[0139] The nesting unit is configured to define a parent node label, nest the optimized rich text data in the parent node label to generate nested rich text data;
[0140] The parsing unit is configured to call a libxml system library and traverse and parse the nested rich text data according to the parent node tag, to generate parsed rich text data.
[0141] In one embodiment, the parsing unit further comprises:
[0142] The sub-node text obtaining sub-unit is configured to perform node searching on the nested rich text data, to obtain all sub-node texts in the nested rich text data.
[0143] The sub-node text defining sub-unit is configured to define all the sub-node texts, to generate defined sub-node texts.
[0144] The objectizing sub-unit is configured to objectize the defined sub-node texts according to a preset format, to generate parsed rich text data.
[0145] In one embodiment, the objectizing sub-unit is further configured to:
[0146] create an xmlDocPtr object document according to the defined sub-node texts, initialize the xmlDocPtr object document according to an NSData, to generate an initialized object document, wherein xmlDocPtr is a pointer to a parsed document, the NSData is a function for converting an html string into a data type in iOS, and htmlReadMemory is an open source method provided in a libxml library in an iOS system, and is used to extract a structure body containing xml information from the NSData;
[0147] determine whether the xmlDocPtr object in the initialized object document is empty;
[0148] when the xmlDocPtr object in the initialized object document is empty, return nil;
[0149] when the xmlDocPtr object in the initialized object document is not empty, create a context pointer of an xmlXpathContextPt type according to the initialized object document, wherein xmlXpathContextPt is a pointer used to define an Xpath context, and Xpath language is an XML language path, and is a language used to determine the position of a part in an XML document;
[0150] determine whether the initialized object document is empty;
[0151] when the initialized object document is empty, return nil;
[0152] Otherwise, query according to the query word in the added parent node and the context pointer, generate an xmlXpathObjectPtr object XpathObj, wherein the xmlXpathObjectPtr is an object pointer used to define an Xpath;
[0153] Judge whether the object XpathObj is empty or not;
[0154] When the object XpathObj is empty, return and release the context pointer;
[0155] Otherwise, create a node set pointer xmlNodeSetPtr object nodes according to the nodesetval of the object XpathObj, wherein the node set contains an element number nodeNr and a node data nodeTab array, and xmlNodeSetPtr is a pointer pointing to the node object nodes;
[0156] Judge whether the object nodes is empty or not;
[0157] When the object nodes is empty, return and release the object XpathObj and the context;
[0158] When the object nodes is not empty, create a variable array NSMutableArray object resultNodes;
[0159] According to the object resultNodes, traverse the node number nodeNr in the object nodes;
[0160] Attribute parsing is performed on the object xmlModePtr in the corresponding node data nodeTab array to generate a parsed array;
[0161] Combine the nodeName node name, nodeContent content, attributeName attribute and raw label contained rich text parameters in the parsed array into a node dictionary resultForNode, and add the node dictionary resultForNode to the object resultNodes to generate a resultNodes array;
[0162] Object assignment is performed on the dictionary data in the resultNodes array to generate an object array.
[0163] Reference Figure 3 In the embodiments of the present application, a computer device is also provided, which can be a server, and the internal structure of the computer device can be as shown in Figure 3The computer device includes a processor, a memory, a network interface and a database connected by a system bus. The processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operating system and the computer program in the non-volatile storage medium. The database of the computer device is configured to store parsing data of the rich text label based on the IOS platform. The network interface of the computer device is configured to communicate with an external terminal through a network connection. The computer program is executed by the processor to implement a rich text label parsing method based on the IOS platform, and the method includes:
[0164] obtaining rich text data and preprocessing the rich text data to generate preprocessed rich text data;
[0165] optimizing the preprocessed rich text data according to a preset optimization rule to generate optimized rich text data;
[0166] defining a parent node label and calling a preset system library to traverse and parse the optimized rich text data with the parent node label to generate parsed rich text data.
[0167] In one embodiment, the step of preprocessing the rich text data to generate preprocessed rich text data includes:
[0168] determining whether the source of the rich text data is a preset source;
[0169] if yes, normalizing the rich text data to generate normalized text data;
[0170] if no, determining whether the rich text data contains a translated character;
[0171] when the rich text data contains the translated character, translating the translated character into a normal character to generate translated rich text data;
[0172] using the normalized text data or the translated rich text data as the preprocessed rich text data.
[0173] In one embodiment, the step of optimizing the preprocessed rich text data according to a preset optimization rule includes:
[0174] based on NSScanner, extracting text in a preset string identifier group in the preprocessed rich text data to generate a string, wherein the preset string identifier group includes a start identifier and an end identifier, and the last character of the string is the end identifier;
[0175] determining whether the string contains the beginning identifier;
[0176] when the string contains the beginning identifier, grouping the string with the beginning identifier as a division point to generate a plurality of first sub-strings and a second sub-string, wherein the end identifier is contained in the last end of the second sub-string;
[0177] determining whether the second sub-string is a label type;
[0178] when the second sub-string is a label type, converting the remaining beginning identifiers in the string except the first beginning identifier into a preset alternative identifier;
[0179] when the second sub-string is not a label type, converting all the beginning identifiers in the string into a preset alternative identifier.
[0180] In an embodiment, the step of defining a parent node label and calling a preset system library to traverse and parse the optimized rich text data with the parent node label to generate parsed rich text data, comprises:
[0181] defining a parent node label, embedding the optimized rich text data in the parent node label to generate nested rich text data;
[0182] calling a libxml system library and traversing and parsing the nested rich text data with the parent node label to generate parsed rich text data.
[0183] In an embodiment, the step of traversing and parsing the nested rich text data with the parent node label to generate parsed rich text data, comprises:
[0184] performing node searching on the nested rich text data to obtain all the child node texts in the nested rich text data;
[0185] defining all the child node texts to generate defined child node texts;
[0186] objectifying the defined child node texts in a preset format to generate parsed rich text data.
[0187] In an embodiment, the step of objectifying the defined child node texts in a preset format, comprises:
[0188] According to the definition of the sub-node text, an xmlDocPtr object document is created, according to the NSData, an htmlReadMemory method is called to initialize the xmlDocPtr object document, and an initialized object document is generated, wherein the xmlDocPtr is a pointer for parsing a document, the NSData is a function for converting an html string into a data type in iOS, and the htmlReadMemory is an open source method provided in a libxml library in an iOS system and used for extracting a structure body containing xml information from the NSData;
[0189] It is judged whether the xmlDocPtr object in the initialized object document is empty or not;
[0190] When the xmlDocPtr object in the initialized object document is empty, nil is returned;
[0191] When the xmlDocPtr object in the initialized object document is not empty, a context pointer of an xmlXpathContextPt type is created according to the initialized object document, wherein the xmlXpathContextPt is a pointer for defining an Xpath context, and the Xpath language is an XML language path and is a language for determining the position of a part in an XML document;
[0192] It is judged whether the initialized object document is empty or not;
[0193] When the initialized object document is empty, nil is returned;
[0194] Otherwise, according to the query word in the added parent node and the context pointer, an xmlXpathObjectPtr object XpathObj is generated, wherein the xmlXpathObjectPtr is an object pointer for defining an Xpath;
[0195] It is judged whether the object XpathObj is empty or not;
[0196] When the object XpathObj is empty, the context pointer is returned and released;
[0197] Otherwise, according to the pointing structure body nodesetval of the object XpathObj, a node set pointer xmlNodeSetPtr object nodes is created, wherein the node set contains an element number nodeNr and a node data nodeTab array, and the xmlNodeSetPtr is a pointer for pointing to the node object nodes;
[0198] determining whether the object nodes is empty or not;
[0199] when the object nodes is empty, returning and releasing the object XpathObj and context;
[0200] when the object nodes is not empty, creating a variable array NSMutableArray object resultNodes;
[0201] traversing the number of nodes nodeNr in the object nodes according to the object resultNodes;
[0202] performing attribute analysis on the object xmlModePtr in the corresponding node data nodeTab array to generate an analysis array;
[0203] combining the nodeName node name, nodeContent content, attributeName attribute and raw label contained rich text parameters in the analysis array into a node dictionary resultForNode, and adding the node dictionary resultForNode to the object resultNodes to generate the resultNodes array;
[0204] performing object assignment on the dictionary data in the resultNodes array to generate an object array.
[0205] In one embodiment, before the step of performing object assignment on the dictionary data in the resultNodes array, the method comprises:
[0206] replacing the placeholder in the dictionary data with the start identifier, and updating the resultNodes array.
[0207] Those skilled in the art can understand that, Figure 3 The structure shown in FIG. 6 is only a block diagram of part of the structure related to the scheme of the present application, and does not constitute a limitation on the computer device to which the scheme of the present application is applied.
[0208] An embodiment of the present application further provides a computer readable storage medium, which stores a computer program. The computer program is executed by a processor to implement a rich text label analysis method based on an IOS platform. It can be understood that the computer readable storage medium in the embodiment can be a volatile readable storage medium or a non-volatile readable storage medium.
[0209] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer readable storage medium, and the computer program can implement the above-mentioned IOS platform-based rich text label parsing method when executed by a processor. The method comprises the following steps:
[0210] Obtaining rich text data and preprocessing the rich text data to generate preprocessed rich text data;
[0211] Optimizing the preprocessed rich text data according to a preset optimization rule to generate optimized rich text data;
[0212] Defining a parent node label, and calling a preset system library to traverse and parse the optimized rich text data with the parent node label to generate parsed rich text data.
[0213] Any reference in the present application to memory, storage, database, or other medium can include non-volatile and / or volatile storage. Non-volatile memory can include read only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM), or external cache memory. As an illustration and not a limitation, RAM is available in many forms such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (SSRSDRAM), enhanced SDRAM (ESDRAM), Synchlink DRAM (SLDRAM), Rambus DRAM (RDRAM), direct Rambus dynamic RAM (DRDRAM), and Rambus dynamic RAM (RDRAM).
[0214] It should be noted that in this document, the terms "comprising", "including", or any other variant thereof are intended to cover non-exclusive inclusions, so that a process, device, article, or method that includes a list of elements not only includes those elements, but also includes other elements not explicitly listed, or inherent to such a process, device, article, or method. Without more limitations, the element defined by the statement "comprising a" does not exclude the presence of additional identical elements in the process, device, article, or method that includes the element.
[0215] The above merely describes the preferred embodiments of the present application, and is not intended to limit the patent scope of the present application. Any equivalent structure or equivalent process transformation, or direct or indirect application in other related technical fields, which is made according to the content of the present application specification and drawings, is also included in the patent protection scope of the present application.
Claims
1. A rich text tag parsing method based on the iOS platform, characterized in that, include: Acquire rich text data and preprocess the rich text data to generate preprocessed rich text data; The preprocessed rich text data is optimized according to preset optimization rules to generate optimized rich text data; Define a parent node label and call a preset system library to traverse and parse the optimized rich text data using the parent node label to generate parsed rich text data; The steps of defining parent node labels and calling a preset system library to traverse and parse the optimized rich text data using the parent node labels to generate parsed rich text data include: Define a parent node label, and nest the optimized rich text data in the parent node label to generate nested rich text data; The libxml system library is invoked, and the nested rich text data is traversed and parsed using the parent node tag to generate parsed rich text data. The step of traversing and parsing the nested rich text data using the parent node tags to generate parsed rich text data includes: Perform a node lookup on the nested rich text data to obtain all child node text in the nested rich text data; Define all the child node texts to generate the definition child node text; The defined child node text is objectified according to a preset format to generate parsed rich text data; The step of objectifying the defined child node text according to a preset format includes: Based on the defined child node text, an xmlDocPtr object document is created. Based on NSData, the htmlReadMemory method is called to initialize the xmlDocPtr object document and generate an initialized object document. Here, xmlDocPtr is a pointer to the parsed document, NSData is a function that converts an HTML string into a data type in iOS, and htmlReadMemory is an open-source method provided by the libxml library in the iOS system, used to extract a structure containing XML information from NSData. Determine whether the xmlDocPtr object in the initialized object document is empty; If the xmlDocPtr object in the initialized object document is empty, return null; When the xmlDocPtr object in the initialization object document is not null, a context pointer of type xmlXpathContextPt is created according to the initialization object document. Here, xmlXpathContextPt is used to define a pointer to an XPath context. XPath is an XML language path, which is a language used to determine the location of a part in an XML document. Determine whether the document of the initialized object is empty; If the initialized object document is empty, return nil; Otherwise, a query is performed based on the query term in the added parent node and the context pointer to generate an xmlXpathObjectPtr object XpathObj, where the xmlXpathObjectPtr is an object pointer used to define an XPath. Determine whether the object XpathObj is empty; When the object XpathObj is null, return and release the context pointer; Otherwise, create a node set pointer xmlNodeSetPtr object nodes based on the nodesetval structure pointed to by the object XpathObj, wherein the node set contains the number of elements nodeNr and the node data nodeTab array, and xmlNodeSetPtr is a pointer to the node object nodes. Determine if the object nodes is empty; If the nodes object is null, return and release the XpathObj object and context. When the node object is not empty, create a mutable array NSMutableArray object resultNodes; Iterate through the number of elements in the object nodes based on the object resultNodes, and find the nodeNr count. Perform attribute parsing on the object xmlModePtr within the corresponding node data nodeTab array to generate a parsed array; The node name (nodeName), nodeContent content, attributeName attribute, and rich text parameters contained in the raw tag within the parsed array are combined into a node dictionary (resultForNode), and added to the object (resultNodes) to generate the resultNodes array. The dictionary data in the resultNodes array is assigned to objects to generate an object array.
2. The rich text tag parsing method based on the iOS platform as described in claim 1, characterized in that, The step of preprocessing the rich text data to generate preprocessed rich text data includes: Determine whether the source of the rich text data is a preset source; If so, the rich text data is then normalized to generate normalized text data; If not, determine whether the rich text data contains transcribing characters; If the rich text data contains the translated character, then the translated character is translated into ordinary characters to generate translated rich text data; The standard text data or the translated rich text data is used as the preprocessed rich text data.
3. The rich text tag parsing method based on the iOS platform as described in claim 1, characterized in that, The step of optimizing the preprocessed rich text data according to preset optimization rules includes: Based on NSScanner, the text within the preset string identifier group in the preprocessed rich text data is extracted and a string is generated. The preset string identifier group includes a start identifier and an end identifier, and the last one of the strings is the end identifier. Determine whether the string contains the starting identifier; When the string contains the start identifier, the string is grouped with the start identifier as the dividing point to generate several groups of first substrings and one group of second substrings, wherein the end identifier is contained at the very end of the second substring; Determine if the second substring is a tag type; When the second substring is a tag type, the remaining starting identifiers in the string, except for the first starting identifier, are converted into preset replacement identifiers; When the second substring is not a tag type, all the starting identifiers in the string are converted to preset replacement identifiers.
4. The rich text tag parsing method based on the iOS platform as described in claim 3, characterized in that, Before the step of assigning object values to the dictionary data in the resultNodes array, the following steps are included: Replace the substitute identifier contained in the dictionary data with the starting identifier, and update the resultNodes array.
5. A rich text tag parsing device based on the iOS platform, used to implement the method described in any one of claims 1-4, characterized in that, include: The rich text data acquisition module is used to acquire rich text data and preprocess the rich text data to generate preprocessed rich text data. The optimization module is used to optimize the preprocessed rich text data according to preset optimization rules to generate optimized rich text data. The rich text data parsing module is used to define parent node tags and call a preset system library to traverse and parse the optimized rich text data using the parent node tags to generate parsed rich text data.
6. A computer device, characterized in that, The method includes a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the rich text tag parsing method based on the iOS platform as described in any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that, It stores a computer program, which, when executed by a processor, implements the steps of the rich text tag parsing method based on the iOS platform as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Rich text data processing method and device, electronic equipment and computer storage medium
CN111274760A