Perl SOAP :: LITE可以从WCF解析对象数组

时间:2011-11-21 03:18:18

标签: arrays wcf perl object

我遇到来自WCF服务的排列值的小问题。我用WCF Storm测试了WCF。但是我需要它在perl中。我可以得到不在数组中的对象部分,但是当我尝试获取数组值时,它似乎跳转到第二个值。因为我可以看到界面2但是看不到任何存储空间。任何建议都非常有帮助。

#!/usr/bin/perl
package main;
use SOAP::Lite;
# Variables
my $url = 'http://<MyServerName>/Services/DCSP/DCSP.ServerManagement.svc?wsdl';
my $url_debug = 'http://localhost:11040/Service1.svc?wsdl';
my $uri = 'http://tempuri.org/';
my $xmlns = 'http://schemas.datacontract.org/2004/07/WcfService1';
my $selectedOS = 'Solaris';
# Setup Network Connection
my $soap = SOAP::Lite
-> uri($uri)
-> on_action(sub { sprintf '%sIServerManagement/%s', @_ })
-> proxy($url)
->autotype(0)->readable(1);

my $response = $soap->serverExists(SOAP::Data->new(name => '_serverNamee', value => '<ServerName>'));
# Print the Result
if ($response->fault)
{
 die $response->faultstring;
}
else
{
if($response->result > 0) {
        my $serverID = $response->result;
        my $buildserver = $soap->getServerById(SOAP::Data->new(name => '_id', value => $serverID));
        my $cpus = $buildserver->valueof('//*/CPUs');

        my $host = $buildserver->valueof('//*/Host');
        my $hostname = $buildserver->valueof('//*/HostName');
        my $memoryMB = $buildserver->valueof('//*/MemoryMB');
        my $OS = $buildserver->valueof('//*/OperatingSystem');
        my $stack = $buildserver->valueof('//*/Stack');
        my $status = $buildserver->valueof('//*/Status');
        my $tier = $buildserver->valueof('//*/Tier');
        my $arch = $buildserver->valueof('//*/architecture');
        my @interfaces = $buildserver->valueof('//*/*/systemInterface');
        my @mounts = $buildserver->valueof('//*/*/systemStorage');

        foreach $interface (@interfaces)
        {
                my $ipid = $interface->{'IPID'};
                my $position = $interface->{'Position'};
                my $ipaddress = $soap->getIpAddressByID(SOAP::Data->new(name => '_ID', value => $ipid));

                my $gateway = $ipaddress->valueof('//*/gateway');
                my $mask = $ipaddress->valueof('//*/mask');
                my $vlan = $ipaddress->valueof('//*/vlanID');
                my $ip = $ipaddress->valueof('//*/address');
        }
    foreach $mount (@mounts)
        {
                my $mountpoint = $mount->{'Anchor'};
                my $size = $mount->{'SizeMB'};
        }
}
}

WCF Storm的输出:

<getServerById>
  <MethodParameters>
    <virtualServer>
      <systemInterface attr0="NetworkInterfaceArray" isNull="false">
        <NetworkInterfaceArray0>
          <IPID>4</IPID>
          <Position>0</Position>
          <SystemID>3</SystemID>
          <id>11</id>
          <EntityKey>System.Data.EntityKey</EntityKey>
        </NetworkInterfaceArray0>
        <NetworkInterfaceArray1>
          <IPID isNull="true" />
          <Position>0</Position>
          <SystemID>3</SystemID>
          <id>19</id>
          <EntityKey>System.Data.EntityKey</EntityKey>
        </NetworkInterfaceArray1>
      </systemInterface>
      <systemSpecs>
        <CPUs>1</CPUs>
        <Host><HOST></Host>
        <HostName><HostName</HostName>
        <MemoryMB>2048</MemoryMB>
        <OperatingSystem>Solaris 10</OperatingSystem>
        <Stack>Development</Stack>
        <Status>Approved</Status>
        <Tier>Teir 1 (Front End)</Tier>
        <architecture>T-Series</architecture>
        <id>3</id>
        <EntityKey>System.Data.EntityKey</EntityKey>
      </systemSpecs>
      <systemStorage attr0="StorageArray" isNull="false">
        <StorageArray0>
          <Anchor>/def</Anchor>
          <SizeMB>2048</SizeMB>
          <SystemID>3</SystemID>
          <id>11</id>
          <EntityKey>System.Data.EntityKey</EntityKey>
        </StorageArray0>
      </systemStorage>
    </virtualServer>
  </MethodParameters>
</getServerById>

从调试回复:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <getServerByIdResponse xmlns="http://tempuri.org/">
      <getServerByIdResult xmlns:a="http://schemas.datacontract.org/2004/07/DCSP.DataClasses" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:systemInterface>
          <a:NetworkInterface xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" z:Id="i1">
            <EntityKey xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" z:Id="i2">
              <b:EntityContainerName>DCSPEntities</b:EntityContainerName>
              <b:EntityKeyValues>
                <b:EntityKeyMember>
                  <b:Key>id</b:Key>
                  <b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">11</b:Value>
                </b:EntityKeyMember>
              </b:EntityKeyValues>
              <b:EntitySetName>NetworkInterfaces</b:EntitySetName>
            </EntityKey>
            <a:IPID>4</a:IPID>
            <a:Position>0</a:Position>
            <a:SystemID>3</a:SystemID>
            <a:id>11</a:id>
          </a:NetworkInterface>
          <a:NetworkInterface xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" z:Id="i3">
            <EntityKey xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" z:Id="i4">
              <b:EntityContainerName>DCSPEntities</b:EntityContainerName>
              <b:EntityKeyValues>
                <b:EntityKeyMember>
                  <b:Key>id</b:Key>
                  <b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">19</b:Value>
                </b:EntityKeyMember>
              </b:EntityKeyValues>
              <b:EntitySetName>NetworkInterfaces</b:EntitySetName>
            </EntityKey>
            <a:IPID i:nil="true"/>
            <a:Position>0</a:Position>
            <a:SystemID>3</a:SystemID>
            <a:id>19</a:id>
          </a:NetworkInterface>
        </a:systemInterface>
        <a:systemSpecs xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" z:Id="i5">
          <EntityKey xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" z:Id="i6">
            <b:EntityContainerName>DCSPEntities</b:EntityContainerName>
            <b:EntityKeyValues>
              <b:EntityKeyMember>
                <b:Key>id</b:Key>
                <b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">3</b:Value>
              </b:EntityKeyMember>
            </b:EntityKeyValues>
            <b:EntitySetName>Computers</b:EntitySetName>
          </EntityKey>
          <a:CPUs>1</a:CPUs>
          <a:Host>Host</a:Host>
          <a:HostName>HostName</a:HostName>
          <a:MemoryMB>2048</a:MemoryMB>
          <a:OperatingSystem>Solaris 10</a:OperatingSystem>
          <a:Stack>Development</a:Stack>
          <a:Status>Approved</a:Status>
          <a:Tier>Teir 1 (Front End)</a:Tier>
          <a:architecture>T-Series</a:architecture>
          <a:id>3</a:id>
        </a:systemSpecs>
        <a:systemStorage>
          <a:Storage xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" z:Id="i7">
            <EntityKey xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data" z:Id="i8">
              <b:EntityContainerName>DCSPEntities</b:EntityContainerName>
              <b:EntityKeyValues>
                <b:EntityKeyMember>
                  <b:Key>id</b:Key>
                  <b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">11</b:Value>
                </b:EntityKeyMember>
              </b:EntityKeyValues>
              <b:EntitySetName>Storages</b:EntitySetName>
            </EntityKey>
            <a:Anchor>/def</a:Anchor>
            <a:SizeMB>2048</a:SizeMB>
            <a:SystemID>3</a:SystemID>
            <a:id>11</a:id>
          </a:Storage>
        </a:systemStorage>
      </getServerByIdResult>
    </getServerByIdResponse>
  </s:Body>
</s:Envelope>

1 个答案:

答案 0 :(得分:1)

你的问题是不可能回答的,你需要在你的程序中发布样本回复

有关捕获数据的调试设置,请参阅Re: Soap::Lite and Complex Types with WSDL

$soap->transport->add_handler("request_send",  \&pp_dump );
$soap->transport->add_handler("response_done", \&pp_dump );
sub pp_dump {
    my $content = $_[0]->content('');
    $_[0]->content( pp($content) );
    print $_[0]->as_string,"\n";
    return;
}
sub pp {
    use XML::Twig;
    open my($fh), '>', \my $str;
    no warnings 'newline';
    XML::Twig->new(qw! pretty_print record !)->xparse(@_)->print( $fh );
    return $str;
}

请参阅Re^3: SOAP::Lite - Attribute in array了解调试设置以访问响应

my $xml = <<'__XML__';
<?xml version="1.0" encoding="UTF-8"?>
...
__XML__
my $response = SOAP::Custom::XML::Deserializer->deserialize( $xml );