Creating a Data Object for a Single XML Element

This section provides a tutorial example on how to create a SOAP::Data object to represent a single XML element with text content, attributes and a target namespace.

After learning how to convert a SOAP::Data object into an XML string, let's look the SOAP::Data class again and learn more about a SOAP::Data object.

Here is a summary of rules on creating a SOAP::Data object to represent a single XML element with text content, attributes and a target namespace:

Here is an example of using SOAP::Data to create a single XML element:

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

#- Creating a single XML element as a SOAP::Data object
   $element = SOAP::Data->new();
   $element->name('RegistrationRequest');
   $element->uri('http://www.herongyang.com/Service/');
   $element->prefix('hy');
   $element->attr({'event' => 'OpenGame', 'date' => '2008-08-08'});
   $element->value('Hello from client.');

#- Dumping the element as an XML string
   $serializer = SOAP::Serializer->new();
   $serializer->readable('true');
   $xml = $serializer->serialize($element);
   print $xml;

Execution result of SOAP_Data_Single_Element.pl:

<?xml version="1.0" encoding="UTF-8"?>
<hy:RegistrationRequest
    xmlns:hy="http://www.herongyang.com/Service/"
    date="2008-08-08"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    event="OpenGame"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:type="xsd:string"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    Hello from client.</hy:RegistrationRequest>

The result looks good to me.

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