Use gsoap to manage xml configuration files of applications

Source: Internet
Author: User

Gsoap supports serializing of application's native C and C ++ data structures, which allows you to save and load of XML serialized data structures to and from files.

For example, for a communication server program, we usually need to configure which services it starts. The standard XML format is:

Common XML binding tools include gsoap and codesynthesis XSD. Gsoap is easy to use and widely used. So the gosp is used as an example.

SupportedUTF-8, multibyte character and other data types.

1. Construct XSD

You can use NotePad to write a file.

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <! -- Syntax definition start. The namespace used by the definition of the schema element and the element type is "http://www.w3.org/2000/10/XMLSchema" -->
3 <schema id = "book" xmlns = "http://www.w3.org/2000/10/XMLSchema">
4 <! -- The root element is book -->
5 <element name = "book">
6 <! -- The secondary element is a composite type -->
7 <complextype>
8 <sequence>
9 <! -- The first sub-element of the book is title, whose data type is string and must appear at least once. 0 indicates that this element can not appear -->
10 <element name = "title" type = "string" minoccurs = "1"/>
11 <! -- The second sub-element of book is publisher, whose data type is string and must appear at least once -->
12 <element name = "publisher" type = "string" minoccurs = "1"/>
13 </sequence>
14 <! -- The "book" node has an attribute named ISBN and the data type is string -->
15 <attribute name = "ISBN" type = "unsignedlong" use = "required"/>
16 </complextype>
17 </element>
18 </Schema>

 

In an XML file, we focus on the element.

Based on the above rules, the wsdl2h tool can generate xml files and C ++ class corresponding to XML files

Use the wsdl2h tool in the command line

C: "Win32> wsdl2h book. XSD

** The gsoap WSDL Parser for C and C ++ 1.2.12
** Copyright (c) 2000-2008 Robert van Engelen, genivia Inc.
** All Rights Reserved. This product is provided "as is", without any warranty.

** The gsoap WSDL parser is released under one of the following two licenses:
** GPL or the specified cial license by genivia Inc. Use Option-L for more info.

Saving book. h

Reading Type Definitions from type map file 'typemap. dat'

Reading file 'book. XSD '... done reading 'book. XSD'

To complete the process, compile:
Soapcpp2 book. h

C: "Win32>

 

Related links:

XML binding technology introduction:

An Introduction to XML data binding in C ++

This article introduces the implementation principle of XML binding technology, the advantages of traditional XML parsing technologies such as Dom and sax, and the limitations of XML binding technology.

XML Schema part 0: Primer

XML schema defines the structure, content, and semantics of an XML file. W3C defines a specification for the XML Schema syntax.
Gsoap 2.7.11 User Guide

 

Gsoap user manual. Section 1.4 xml c/C ++ data bindings: Mapping WSDL and XSD to C/C ++ describes how to use the gsoap toolkit to parse and generate xml files based on XSD files. 7.5 how to use the soap serializers and deserializers to save and load application data using XML data bindings

The book. h file is generated. Classes are defined as follows:

Class _ NS1 _ book
{Public:
/// Element title of Type Xs: string.
String * Title 1; // <required element.
/// Element publisher of Type Xs: string.
String * publisher 1; // <required element.
/// Attribute ISBN of Type Xs: Int.
@ Ulong ISBN 1; // <required attribute.
/// A handle to the soap struct that manages this instance (automatically set)
Struct soap * soap;
};

 

NS1 is the default namespace name. If wsdl2h translates multiple XSD files at a time, ns3...

Struct soap is a heap space used to store class members.

@ Indicates that this member is a property value.

If an element is optional, it is represented as a pointer to the object. If the element value is required, it is represented as an object.

 

Use soapcpp2 to generate class definitions

C: "Win32> soapcpp2-I./import-l book. h

** The gsoap stub and skeleton compiler for C and C ++ 2.7.12
** Copyright (c) 2000-2008, Robert van Engelen, genivia Inc.
** All Rights Reserved. This product is provided "as is", without any warranty.

** The gsoap compiler is released under one of the following three licenses:
** GPL, The gsoap Public License, or the specified cial license by genivia Inc.

Saving soapstub. h
Saving soaph. h
Saving soapc. cpp
Saving soapclient. cpp
Saving soapserver. cpp
Saving ns1.nsmap namespace mapping table

Compilation successful

C: "Win32>

 

Parameter description:

Files required in the program:

Stdsoap2.cpp stdsoap2.h ns1.msmap soaph. h soapc. cpp soapstub. h

The generated class is defined as follows:

1 class soap_cmac _ NS1 _ book
2 {
3 public:
4 char * Title;/* required element of Type XSD: string */
5 char * publisher;/* required element of Type XSD: string */
6 int ISBN;/* required attribute */
7 struct soap * soap;/* transient */
8 public:
9 virtual int soap_type () const {return 8;}/* = unique ID soap_type _ NS1 _ book */
10 virtual void soap_default (struct soap *);
11 virtual void soap_serialize (struct soap *) const;
12 virtual int soap_put (struct soap *, const char *, const char *) const;
13 virtual int soap_out (struct soap *, const char *, Int, const char *) const;
14 virtual void * soap_get (struct soap *, const char *, const char *);
15 virtual void * soap_in (struct soap *, const char *, const char *);
16 _ NS1 _ book (): Title (null), publisher (null), ISBN (0), Soap (null ){}
17 virtual ~ _ NS1 _ book (){}
18 };

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.