【问题标题】:How to manipulate data from SOAP XML with regex如何使用正则表达式处理来自 SOAP XML 的数据
【发布时间】:2018-06-04 03:04:50
【问题描述】:

我打算编写一个通过 SOAP 获取数据的脚本。我可以得到一个包含大量以下数据的文件,例如:

 <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns0:GetList_Operation_0Response xmlns:ns0="urn:TEST:AST:Attributes" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:getListValues>
        <ns0:AssetLifecycleStatus>Deployed</ns0:AssetLifecycleStatus>
        <ns0:ReconciliationIdentity>OI-ASDAQWDASDWA</ns0:ReconciliationIdentity>
        <ns0:instanceId>OI-SDWDSDWDSDWD</ns0:instanceId>
        <ns0:ModifiedDate>2017-12-12T03:31:32+01:00</ns0:ModifiedDate>
        <ns0:ClassId>BMC_COMPUTERSYSTEM</ns0:ClassId>
    </ns0:getListValues>
    <ns0:getListValues>
        <ns0:AssetLifecycleStatus>Being Assembled</ns0:AssetLifecycleStatus>
        <ns0:ReconciliationIdentity>OI-ASDQWAWDSADW</ns0:ReconciliationIdentity>
        <ns0:instanceId>OI-SDWDSWDSWDWD</ns0:instanceId>
        <ns0:ModifiedDate>2017-12-10T03:30:21+01:00</ns0:ModifiedDate>
        <ns0:ClassId>BMC_COMPUTERSYSTEM</ns0:ClassId>
    </ns0:getListValues>
    <ns0:getListValues>
        <ns0:AssetLifecycleStatus>Deployed</ns0:AssetLifecycleStatus>
        <ns0:ReconciliationIdentity>OI-ASDWASDWDASDW</ns0:ReconciliationIdentity>
        <ns0:instanceId>OI-SDWDSDWDSDWD</ns0:instanceId>
        <ns0:ModifiedDate>2017-12-12T03:31:31+01:00</ns0:ModifiedDate>
        <ns0:ClassId>BMC_COMPUTERSYSTEM</ns0:ClassId>
    </ns0:getListValues>
</ns0:GetList_Operation_0Response></soapenv:Body></soapenv:Envelope>

您能否建议如何仅提取“AssetLifecycleStatus”之间的状态,例如:已部署。它应该通过这条线的每一部分并将输出提供给新的线。示例:

部署

正在组装

部署

哪种语言是处理此类数据的最佳语言,是 Perl 吗? 感谢您的信息!

【问题讨论】:

    标签: xml linux perl curl soap


    【解决方案1】:

    对于解析 XML,没有真正的最佳语言。 Perl 以其正则表达式而闻名,但使用专用解析器解析 XML 效果要好得多。在 perl 中,XML::LibXML 是一种广泛使用的方法。 Perl还有其他的解析器,还有Python、Ruby、JS等。

    #!/usr/bin/perl
    use strict;
    use warnings;
    use XML::LibXML;
    
    my $doc = XML::LibXML->load_xml(IO => *DATA);
        # can also load with (string => $xml_string) or (location => 'file.xml');
    my @nodes = $doc->getElementsByTagName('ns0:AssetLifecycleStatus');
    
    for my $node (@nodes) {
        print $node->textContent."\n";
    }
    
    
    __DATA__
    <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns0:GetList_Operation_0Response xmlns:ns0="urn:TEST:AST:Attributes" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns0:getListValues>
            <ns0:AssetLifecycleStatus>Deployed</ns0:AssetLifecycleStatus>
            <ns0:ReconciliationIdentity>OI-ASDAQWDASDWA</ns0:ReconciliationIdentity>
            <ns0:instanceId>OI-SDWDSDWDSDWD</ns0:instanceId>
            <ns0:ModifiedDate>2017-12-12T03:31:32+01:00</ns0:ModifiedDate>
            <ns0:ClassId>BMC_COMPUTERSYSTEM</ns0:ClassId>
        </ns0:getListValues>
        <ns0:getListValues>
            <ns0:AssetLifecycleStatus>Being Assembled</ns0:AssetLifecycleStatus>
            <ns0:ReconciliationIdentity>OI-ASDQWAWDSADW</ns0:ReconciliationIdentity>
            <ns0:instanceId>OI-SDWDSWDSWDWD</ns0:instanceId>
            <ns0:ModifiedDate>2017-12-10T03:30:21+01:00</ns0:ModifiedDate>
            <ns0:ClassId>BMC_COMPUTERSYSTEM</ns0:ClassId>
        </ns0:getListValues>
        <ns0:getListValues>
            <ns0:AssetLifecycleStatus>Deployed</ns0:AssetLifecycleStatus>
            <ns0:ReconciliationIdentity>OI-ASDWASDWDASDW</ns0:ReconciliationIdentity>
            <ns0:instanceId>OI-SDWDSDWDSDWD</ns0:instanceId>
            <ns0:ModifiedDate>2017-12-12T03:31:31+01:00</ns0:ModifiedDate>
            <ns0:ClassId>BMC_COMPUTERSYSTEM</ns0:ClassId>
        </ns0:getListValues>
    </ns0:GetList_Operation_0Response></soapenv:Body></soapenv:Envelope>
    

    【讨论】:

      【解决方案2】:

      如果您已经安装了Mojolicious framework,您可以使用ojo 包编写一个简单的Perl 单行代码。之所以称为ojo,是因为当您在命令行中使用-M 时,它将变为-Mojo

      此程序假定在同一目录中有一个名为 foo.xml 的文件与您的 XML 内容。

      $ perl -Mojo -E 'x(f("foo.xml")->slurp)->find("AssetLifecycleStatus")->map(sub{say $_->text})'
      Deployed
      Being Assembled
      Deployed
      

      我会带你完成这个程序。

      perl                               # call the Perl interpreter
        -Mojo                            # load module ojo
        -E '                             # run the following program with all features turned on
          x(                             # ojo function to turn string into Mojo::DOM object
            f("foo.xml")                 # ojo function to create a Mojo::File object
              ->slurp                    # read in the whole file and return content
          )
          ->find("AssetLifecycleStatus") # find all instances of string in DOM with CSS selector
          ->map(                         # iterate and run on all instances ...
            sub {                        # ... this function ...
              say $_->text               # ... output text node of element with newline attached
            }
          )
        '
      

      【讨论】:

      • 当然这不处理 SOAP 部分。你需要一些更复杂的东西。
      【解决方案3】:

      如果您可以使用 XSLT 2.0,请使用此代码。

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          exclude-result-prefixes="xs"
          version="2.0">
      
          <xsl:template match="/">
              <xsl:for-each select="//*:AssetLifecycleStatus">
                  <xsl:text>&#x000A;</xsl:text><xsl:value-of select="."/>
              </xsl:for-each>
          </xsl:template>
      
      
      </xsl:stylesheet>
      

      输出是:

      已部署
      正在组装
      已部署

      【讨论】:

      • 还不知道怎么用,不过我看到我们的Linux机器上安装了xsltproc。
      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      相关资源
      最近更新 更多