SOAP::Serializer - Converting Data Objects to XML

This section provides a tutorial example on how to convert SOAP::Data objects into XML strings, using an undocumented method serialize() in the SOAP::Serializer class.

In order to validate XML structure represented in a SOAP::Data object, we need to find a way to convert, or serialize, SOAP::Data objects into XML strings.

SOAP::Lite 0.710 manual does not mentioning how to convert SOAP::Data objects into XML strings anywhere. But I found this undocumented method, serialize(), on the SOAP::Serializer that will do the job. There are two ways to use serialize():

1. Using serialize() as a class method:

#- Converting a SOAP::Data object $element into an XML string
#- using defaulting settings
   $xml = SOAP::Serializer->serialize($element);

2. Using serialize() as an object method:

#- Converting a SOAP::Data object $element into an XML string
#- using modified settings
   $serializer = SOAP::Serializer->new();
   $serializer->readable('true');
   $xml = $serializer->serialize($element);

Here is an example Perl program that shows you how to convert a SOAP::Data object into an XML string:

#- SOAP_Data_to_XML_String.pl
#- Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
#- All rights reserved
#
   use SOAP::Lite;

#- Creating a SOAP::Data object
   $element = SOAP::Data->new();
   $element->name('HelloRequest');
   $element->value('Hello from client.');

#- Creating a SOAP::Serializer object with new settings
   $serializer = SOAP::Serializer->new();
   $serializer->readable('true');

#- Creating the SOAP::Data object to an XML string
   $xml = $serializer->serialize($element);
   print $xml;

Execution result of SOAP_Data_to_XML_String.pl:

<?xml version="1.0" encoding="UTF-8"?>
<HelloRequest
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xsi:type="xsd:string"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    Hello from client.</HelloRequest>

Notes on the program and the result:

Last update: 2009.

Table of Contents

 About This Book

 Introduction to WSDL 2.0

 WSDL 2.0 Document Structure and Syntax

 WSDL Version 2.0 Part 2: Adjuncts

 WSDL 2.0 Document Examples with SOAP Binding

 WSDL 20 Programming APIs and Testing Tools

 Introduction to WSDL 1.1

 WSDL 1.1 Document Structure and Syntax

 WSDL 1.1 Binding Extension for SOAP 1.1

 soapUI 3.0.1 - Web Service Testing Tool

 WSDL 1.1 and SOAP 1.1 Examples - Document and RPC Styles

 PHP SOAP Extension in PHP 5.3.1

Using WSDL in Perl with SOAP::Lite 0.710

 Introduction of Perl SOAP::Lite 0.710 for WSDL

 Methods on SOAP::Lite 0.710 Client Object

 Calling PRC Methods Defined in WSDL 1.1 Documents

 service() Method Returns New Objects

 SOAP::Data - XML Elements as Data Objects

SOAP::Serializer - Converting Data Objects to XML

 Creating a Data Object for a Single XML Element

 Creating a Data Object for Nested XML Elements

 SOAP::Deserializer - Converting XML to Data Objects

 Calling XML Document Based Web Service

 Using Operation Name as the SOAP Body Element Name

 Using WSDL Document in Java with Axis2 1.4.1

 Using WSDL2Java to Generate Web Service Stub Classes

 WSDL 1.1 Binding Extension for SOAP 1.2

 WSDL 1.1 and SOAP 1.2 Examples - Document and RPC Styles

 SOAP 1.2 Binding - PHP, Java and Perl Clients

 WSDL Related Terminologies

 References

 PDF Printing Version