Method, system, device and storage medium for converting an xml document into objects

By parsing the XSD file of the XML document, using the rule configurator and template engine to generate Java classes, and using the JAXB interface for serialization and deserialization, the problem of unoptimized Java classes generated by JAXB is solved, the conversion efficiency and flexibility are improved, the integration process is simplified, and the system maintenance cost is reduced.

CN119849440BActive Publication Date: 2025-10-17TRAVELSKY TECHNOLOGY LIMITED
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411874134.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-18
Publication Date
2025-10-17
Estimated Expiration
2044-12-18

AI Technical Summary

Technical Problem

The existing Java classes generated by JAXB are not optimized, with low conversion efficiency, poor flexibility, complex integration, and high developer experience requirements when processing large or complex XML documents.

Method used

By parsing the XSD file of the XML document, using the rule configurator and template engine to generate Java classes, and using JAXB's Unmarshaller and Marshaller interfaces for deserialization and serialization operations, it supports lazy loading and streaming processing to simplify the integration process.

Benefits of technology

The conversion efficiency from XSD files to Java objects is improved, the workload and error rate of manual coding are reduced, the generated Java classes are more in line with business needs, the integration process is simplified, the reliability and accuracy of data processing are improved, and the complexity of system maintenance is reduced.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119849440B_ABST
    Figure CN119849440B_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of data processing, and particularly relates to a method, system and device for converting an XML document into an object and a storage medium. The present application aims to solve the technical problems that the JAXB generated Java class is not optimized or cannot meet the developer's requirements, and the process of a large or complex XML document is too complex. The present application comprises the following steps: obtaining an XSD file of an XML document and performing parsing to obtain XSD parsed data; configuring corresponding rule data by using a rule configurator; adding the XSD parsed data and the rule data into a template engine to perform code filling and generate a JAVA class; configuring a JAXB context instance; creating a Unmarshaller instance by using a Unmarshaller interface of the JAXB, and performing a deserialization operation on the XML document based on the Unmarshaller instance to obtain a Java object.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application belongs to the technical field of data processing, and particularly relates to a method, system, device and storage medium for converting an XML document into an object. BACKGROUND

[0002] In the civil aviation industry, with the development of e-commerce, a large amount of information exchange is required between different participants such as airlines, airports, travel agencies, etc., including flight information, price information, reservation information, etc. XML is a relatively powerful semi-structured data with a standard format, so XML documents are used to store and transmit data. In order to ensure that the structure and content of the XML document meet the predetermined specifications, an XML Schema Definition (XSD) is usually used to define the structure of the XML document. However, the XSD file is not convenient to read and understand, so the XSD file needs to be converted.

[0003] In the prior art, Java Architecture for XML Binding (JAXB) is used to achieve the above-mentioned requirements, converting the XSD file into a Java class. And according to the Java class, the XML document is converted into a Java object. In addition, JAXB also supports serialization of Java objects into XML documents.

[0004] The prior art has the following technical problems:

[0005] 1. The existing JAXB-generated Java class is not optimized or cannot meet the developer's requirements.

[0006] 2. There are low efficiency, poor flexibility, and complex integration in the conversion process from the XSD file to the Java object.

[0007] 3. When processing large or complex XML documents, the JAXB API is too complex for developers, requiring developers to have certain experience, resulting in an increase in labor costs. SUMMARY

[0008] The present application provides a method, system, device and storage medium for converting an XML document into an object, aiming to solve the technical problems of the JAXB-generated Java class not being optimized or not meeting the developer's requirements in the prior art, low efficiency, poor flexibility, and complex integration in the conversion process from the XSD file to the Java object, and the JAXB API being too complex for developers when processing large or complex XML documents, requiring developers to have certain experience.

[0009] The technical scheme for solving the above technical problems is as follows: a method for converting an XML document into an object, comprising:

[0010] An XSD file of the XML document is acquired, the XSD file is parsed, and XSD parsed data is obtained;

[0011] Based on the XSD parsed data, corresponding rule data is configured by using a rule configurator;

[0012] The XSD parsed data and the rule data are added to a template engine, code filling is performed by using the template engine, and a JAVA class is generated;

[0013] The JAVA class is used as a class for JAXB binding, and a JAXB context instance is configured;

[0014] Based on the JAXB context instance, an Unmarshaller instance is created by using an Unmarshaller interface of JAXB, and a deserialization operation is performed on the XML document based on the Unmarshaller instance, so that a JAVA object is obtained.

[0015] Further, the above further comprises:

[0016] Based on the JAXB context instance, a Marshaller instance is created by using a Marshaller interface of JAXB;

[0017] Based on the Marshaller instance, a serialization operation is performed on the JAVA object, so that an XML document is obtained.

[0018] Further, the parsed data comprises elements, attributes and data types.

[0019] Further, the rule data comprises use of annotations and mapping of specific data types.

[0020] Further, in the configuration of the JAXB context instance, the created JAXB context instance is used.

[0021] Further, in the deserialization operation on the XML document, a lazy loading method is used to manage loading of data.

[0022] Further, in the deserialization operation on the XML document, a streaming processing method is used to read the XML document.

[0023] In a second aspect, the present application also provides a system for converting an XML document into an object, comprising:

[0024] The data analysis module is configured to obtain an XSD file of the XML document, analyze the XSD file, and obtain XSD analysis data.

[0025] The rule configuration module is configured to configure corresponding rule data based on the XSD analysis data by using a rule configuration tool.

[0026] The template engine module is configured to add the XSD analysis data and the rule data into a template engine, fill in codes by using the template engine, and generate a JAVA class.

[0027] The JAXB configuration module is configured to configure a JAXB context instance by taking the JAVA class as a JAXB bound class.

[0028] The deserialization module is configured to create a Unmarshaller instance by using a Unmarshaller interface of JAXB based on the JAXB context instance, and perform a deserialization operation on the XML document based on the Unmarshaller instance to obtain a JAVA object.

[0029] In a third aspect, the present application provides an electronic device including a memory, a processor, and a computer program stored in the memory and executable on the processor, and the processor implements the method for converting an XML document into an object when executing the computer program.

[0030] In a fourth aspect, the present application provides a computer readable storage medium, and the computer readable storage medium stores a computer program, and the computer program is executable on a processor to implement the method for converting an XML document into an object.

[0031] Compared with the prior art, the present application has the following advantages:

[0032] 1. The present application significantly improves the conversion efficiency from an XSD file to a JAVA object by automatically analyzing the XSD file and generating codes, and reduces the workload and error rate of manual coding.

[0033] 2. The present application supports user-defined annotations and rule configurations, so that the generated JAVA class can better meet the needs of specific business scenarios, and improves the flexibility and customizability of the codes.

[0034] 3. The generated JAVA class of the present application is directly compatible with the JAXB framework, which simplifies the integration process of serialization and deserialization between the XML document and the JAVA object, and reduces the technical threshold and development cost.

[0035] 4. The application generates Java classes based on the strict definition of XSD files, ensuring the structural consistency between XML documents and Java objects, and improving the reliability and accuracy of data processing.

[0036] 5. The application can quickly update Java classes by re-running the code generation process when the XSD files are updated, reducing the complexity and cost of system maintenance.

[0037] Other features and advantages of the present application will be set forth in the following description, and in part will become apparent to those skilled in the art upon examination of the following or can be learned by practice of the application. The objects and other advantages of the application can be realized and attained by the structure particularly pointed out in the written description and claims hereof as well as the appended drawings. BRIEF DESCRIPTION OF DRAWINGS

[0038] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings needed to be used in the embodiments or prior art description. Obviously, the drawings in the following description are some embodiments of the present application, and for those skilled in the art, other drawings can also be obtained without creative labor based on these drawings.

[0039] Figure 1 A flowchart of a method for converting an XML document into an object according to an embodiment of the present application is shown;

[0040] Figure 2 A flowchart of Java class generation according to an embodiment of the present application is shown;

[0041] Figure 3 A flowchart of XML data deserialization into a Java object according to an embodiment of the present application is shown;

[0042] Figure 4 A flowchart of Java object serialization into XML data according to an embodiment of the present application is shown;

[0043] Figure 5 A structural diagram of a system for converting an XML document into an object according to an embodiment of the present application is shown;

[0044] Figure 6 A structural diagram of an electronic device according to an embodiment of the present application is shown. DETAILED DESCRIPTION

[0045] In order to make the purposes, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, but not all the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work fall within the scope of the present application.

[0046] Figure 1 A flowchart of a method for converting an XML document into an object according to an embodiment of the present application is shown in FIG. 1. As shown in FIG. 1, the method for converting an XML document into an object according to an embodiment of the present application comprises the following steps. Figures 1-3

[0047] Obtaining an XSD file of an XML document, parsing the XSD file to obtain XSD parsed data;

[0048] Based on the XSD parsed data, configuring corresponding rule data by using a rule configurator;

[0049] Adding the XSD parsed data and the rule data into a template engine, filling codes by using the template engine, and generating a JAVA class;

[0050] Taking the JAVA class as a class for JAXB binding, and configuring a JAXB context instance;

[0051] Based on the JAXB context instance, creating a Marshaller instance by using a Marshaller interface of JAXB, and based on the Marshaller instance, performing a deserialization operation on the XML document to obtain a JAVA object.

[0052] Optionally, as shown in FIG. 1, the method further comprises the following steps. Figure 4

[0053] Based on the JAXB context instance, creating a Marshaller instance by using a Marshaller interface of JAXB;

[0054] Based on the Marshaller instance, performing a serialization operation on the JAVA object to obtain an XML document.

[0055] Optionally, the parsed data comprises elements, attributes and data types.

[0056] ​​In the embodiment, the XSD parser identifies complex types (complexType), simple types (simpleType), elements (element) and attributes (attribute), and also includes combination modes such as sequence (sequence), choice (choice) and all (all).

[0057] Optionally, the rule data includes the use of annotations and the mapping of specific data types.

[0058] In the embodiment, the use of custom annotations, the mapping of specific data types and the like are used to set additional metadata information for the generated Java class.

[0059] Optionally, the content of the code filling includes class definition, field, getter / setter method and JAXB annotation.

[0060] Optionally, the method further includes:

[0061] The JAVA class is saved to a specified directory, and file naming is managed, and a Jar package is generated.

[0062] Optionally, the configuration of the JAXB context instance further includes the use of the created JAXB context instance.

[0063] In the embodiment, since the creation of the JAXB context instance is a relatively expensive operation, the same JAXBContext instance can be managed and shared through a singleton mode or a dependency injection framework, so that it is not recreated every time.

[0064] Optionally, in the process of the deserialization operation on the XML document, a lazy loading method is used to manage the loading of data.

[0065] In the embodiment, when the XML document is very large or contains a large number of associated objects, loading all the data into the memory at one time can cause high memory consumption. The embodiment uses the lazy loading method to load a certain attribute or associated object only when it is first accessed, thereby reducing the memory occupation during the initial parsing. Through lazy loading, the basic object structure can be quickly built at the application startup, and the loading of detailed information is delayed until it is actually needed, so that the application startup speed can be accelerated.

[0066] Optionally, in the process of the deserialization operation on the XML document, a streaming processing method is used to read the XML document.

[0067] In this embodiment, when processing very large XML documents, loading all the content into memory at once can lead to high memory consumption. This embodiment therefore reads the XML document element by element through streaming, mapping the current element to a Java object only when needed, thus greatly reducing memory usage. And through streaming, elements of interest can be processed as soon as they are encountered, without waiting for the entire document to be parsed. This can speed up the retrieval process of specific information.

[0068] In this embodiment, Maven projects are used to parse XSD files and generate Java code, which is then packaged into JAR files and pushed to the repository. Below is a detailed description of each step:

[0069] 1. Create a new Maven project:

[0070] Open the IDE to create a new Maven project.

[0071] Configure the dependencies and plugins of the project in pom.xml, ensuring that the plugin for generating Java classes from XSD is included. In the pom.xml configuration, add the following: <packagename>and other configuration items to set up custom annotations, package names, etc.

[0072] 2. Ensure that the XSD file path is correct and meets the document specification requirements

[0073] Place the XSD files in the specified directory and ensure they follow the XML Schema definition standard.

[0074] Verify that the XSD file can be correctly parsed, which can be tested through command line tools or other methods.

[0075] 3. Configure custom adapters as needed

[0076] Determine if the XSD file needs to be configured with a custom adapter, and if so, configure it; adapters are mainly used to process business logic and convert certain special types of data, making it easier to integrate with business logic. Adapters need to be developed according to actual business needs.

[0077] 4. Execute Maven compilation instructions

[0078] Execute the mvn clean install command to clean up old build files, download all necessary dependencies, compile source code, run any unit tests, and finally package the compiled code into a JAR package and push it to the repository.

[0079] After obtaining the Java class, use JAXB for serialization or deserialization, the specific steps include:

[0080] 1. Configure JAXB context

[0081] The JAXB configurator configures the JAXB context instance based on the generated Java class file;

[0082] 2. Serialize Java objects to XML documents

[0083] Use the serializer to call the Marshaller interface and create a Marshaller object. Then call the marshal method, passing in the Java object to be serialized and the output destination;

[0084] 3. Deserialize XML documents to Java objects

[0085] Use the deserializer to call the Unmarshaller interface. Create a Unmarshaller object, then call the unmarshal method, passing in the XML document, and get the returned Java object.

[0086] 4. Embed business logic

[0087] The serialization / deserialization process is embedded into the business logic to realize the exchange, storage or processing of data, that is, the generated Jar file is introduced into the business logic, and the method of steps 2 and 3 is called to realize the serialization / deserialization.

[0088] In the embodiment, since the creation of the JAXB context is time-consuming, the core function of the findContext method is cached by a caching technique, the created JAXB context is cached, repeated creation of the JAXB context is avoided, and the efficiency of the serialization / deserialization is greatly improved.

[0089] Based on the same principle as the method shown in Figure 1 The embodiment of the application also provides a system for converting an XML document into an object, as shown in Figure 5 The system comprises:

[0090] A data parsing module is configured to obtain an XSD file of the XML document, parse the XSD file, and obtain XSD parsed data.

[0091] A rule configuration module is configured to configure corresponding rule data by using a rule configurator based on the XSD parsed data.

[0092] A template engine module is configured to add the XSD parsed data and the rule data into a template engine, fill in code by using the template engine, and generate a JAVA class.

[0093] A JAXB configuration module is configured to configure a JAXB context instance by taking the JAVA class as a JAXB bound class.

[0094] A deserialization module is configured to create a Unmarshaller instance by using a Unmarshaller interface of JAXB based on the JAXB context instance, and perform a deserialization operation on the XML document based on the Unmarshaller instance to obtain a JAVA object.

[0095] The system for converting an XML document into an object can execute the method for converting an XML document into an object, and the implementation principles are similar.

[0096] Among them, the above-mentioned system for converting XML documents into objects can be a computer program (including program code) running on a computer device, for example, the system for converting XML documents into objects is an application software; the application software can be used to execute the corresponding steps in the method provided in the embodiment of the present invention.

[0097] In some embodiments, the system for converting XML documents into objects provided by the embodiments of the present invention can be implemented in a combination of software and hardware. As an example, the system for converting XML documents into objects provided by the embodiments of the present invention can be a processor in the form of a hardware decoding processor, which is programmed to execute the method for converting XML documents into objects provided by the embodiments of the present invention. For example, the processor in the form of a hardware decoding processor can adopt one or more application-specific integrated circuits (ASICs), DSPs, programmable logic devices (PLDs), complex programmable logic devices (CPLDs), field-programmable gate arrays (FPGAs), or other electronic components.

[0098] The modules involved in the embodiments of the present invention may be implemented in software or hardware, wherein the name of a module does not necessarily limit the module itself.

[0099] Based on the same principle as the method shown in the embodiments of the present invention, an electronic device is also provided in the embodiments of the present invention, which may include but is not limited to: a processor and a memory; the memory is used to store computer programs; the processor is used to execute the method shown in any embodiment of the present invention by calling the computer program.

[0100] In an alternative embodiment, an electronic device is provided, such as Figure 6 As shown, Figure 6 The electronic device shown includes a processor and a memory. The processor and the memory are connected, for example, via a bus. Optionally, the electronic device may further include a transceiver, which can be used for data exchange between the electronic device and other electronic devices, such as data transmission and / or data reception. It should be noted that in actual applications, there is not limited to one transceiver, and the structure of the electronic device does not constitute a limitation on the embodiments of the present invention.

[0101] The processor can be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array) or other programmable logic device, transistor logic device, hardware component, or any combination thereof. It can implement or execute various exemplary logical blocks, modules and circuits described in combination with the present disclosure. The processor can also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, and the like.

[0102] The bus can include a path that transmits information between the above-mentioned components. The bus can be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, or the like. The bus can be divided into an address bus, a data bus, a control bus, and the like. For convenience of representation, Figure 6 In the figure, only one thick line is used to represent the bus, but it does not mean that there is only one bus or only one type of bus.

[0103] The memory can be a ROM (Read Only Memory) or other type of static storage device that can store static information and instructions, a RAM (Random Access Memory) or other type of dynamic storage device that can store information and instructions, an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, an optical disk storage (including a compact disk, a laser disk, an optical disk, a digital versatile disk, a Blu-ray disk, and the like), a magnetic disk storage medium or other magnetic storage device, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and capable of being accessed by a computer, but not limited thereto.

[0104] The memory is used to store application program code (computer program) for executing the scheme of the present application, and is controlled by the processor to execute. The processor is used to execute the application program code stored in the memory to realize the content shown in the foregoing method embodiments.

[0105] The electronic device can also be a terminal device, Figure 6 The electronic device shown is merely an example and should not impose any limitation on the functions and use range of the embodiments of the present application.

[0106] The computer readable storage medium stores a computer program, and when the computer program runs on a computer, the computer can execute the corresponding content in the foregoing method embodiments.

[0107] According to another aspect of the present application, a computer program product or computer program is also provided, which includes computer instructions stored in a computer readable storage medium. A processor of a computer device reads the computer instructions from the computer readable storage medium, and the processor executes the computer instructions, so that the computer device executes the method provided in the various embodiment implementation manners described above.

[0108] Computer program code for carrying out operations of the present application can be written in one or more programming languages or combinations of languages including object oriented programming languages such as Java, Smalltalk, C++ or conventional procedural programming languages such as "C" or similar programming languages. The program code can execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the latter scenario, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or the connection can be made to an external computer (for example, through the Internet using an Internet Service Provider).

[0109] It should be understood that the flow diagrams and block diagrams in the drawings are presented to illustrate the architecture, functionality, and operation of possible implementations of methods and computer program products according to various embodiments of the present application. In this regard, each block in the flow diagrams and block diagrams can represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical functions. It should also be noted that in some alternative implementations, the functions noted in the blocks can occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently or the blocks can sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and / or flow diagrams, and combinations thereof, can be implemented by special purpose hardware-based systems that perform the specified functions or operations, or combinations of special purpose hardware and

[0110] The computer readable storage medium of embodiments of the present application may, for example, be but is not limited to an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of the computer readable storage medium can include, but are not limited to, an electrical connection having one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present application, the computer readable storage medium can be any tangible medium that can contain or store a program for use by or in connection with an instruction execution system, apparatus, or device.

[0111] The computer readable storage medium described above can bear one or more programs, which, when executed by the electronic device, cause the electronic device to perform the method shown in the above embodiments.

[0112] The above description merely illustrates the preferred embodiments of the present application and the principles of the technology applied. It should be understood that the disclosed scope of the present application is not limited to the technical solutions formed by the specific combinations of the above technical features, and should also cover other technical solutions formed by any combinations of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above technical features can be replaced with the technical features disclosed in the present application (but not limited to) having similar functions to form technical solutions.< / packagename>

Claims

1. A method for converting an XML document into an object, characterized in that: The method comprises: Obtain an XSD file of the XML document, parse the XSD file, and obtain XSD parsed data; Based on the XSD parsed data, corresponding rule data is configured using a rule configurator; Adding the XSD parsed data and the rule data to a template engine, performing code filling using the template engine, and generating a JAVA class; Use the Java class as a JAXB binding class and configure a JAXB context instance; Based on the JAXB context instance, an Unmarshaller instance is created using the Unmarshaller interface of JAXB, and based on the Unmarshaller instance, a deserialization operation is performed on the XML document to obtain a Java object.

2. A method for converting an XML document into an object according to claim 1, characterized in that: Also includes: Based on the JAXB context instance, create a Marshaller instance using the JAXB Marshaller interface; Based on the Marshaller instance, a serialization operation is performed on the Java object to obtain an XML document.

3. The method for converting an XML document into an object according to claim 1, wherein: The parsed data includes: elements, attributes and data types.

4. The method for converting an XML document into an object according to claim 1, wherein: The rule data includes: usage of annotations and mapping of specific data types.

5. The method for converting an XML document into an object according to claim 1, wherein: Configuring a JAXB context instance also includes using an already created JAXB context instance.

6. The method for converting an XML document into an object according to claim 1, wherein: During the deserialization operation on the XML document, a lazy loading method is used to manage the loading of data.

7. The method for converting an XML document into an object according to claim 1, wherein: During the deserialization operation on the XML document, a streaming processing method is used to read the XML document.

8. A system for converting XML documents into objects, characterized in that: include: A data parsing module is used to obtain an XSD file of an XML document, parse the XSD file, and obtain XSD parsed data; A rule configuration module, configured to parse data based on the XSD and configure corresponding rule data using a rule configurator; A template engine module is used to add the XSD parsed data and the rule data into a template engine, perform code filling using the template engine, and generate a JAVA class; A JAXB configuration module is used to use the Java class as a JAXB binding class and configure a JAXB context instance; The deserialization module is used to create an Unmarshaller instance based on the JAXB context instance and using the Unmarshaller interface of JAXB, and to deserialize the XML document based on the Unmarshaller instance to obtain a Java object.

9. An electronic device, characterized in that: The method comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method according to any one of claims 1 to 7 when executing the computer program.

10. A computer storage medium, characterized in that The computer storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Method for converting XML Schema document into Java codes

    CN108519964A

  • Systems and methods for processing extensible markup language data during runtime

    US20170262261A1