【问题标题】:XML manipulation using perl使用 perl 进行 XML 操作
【发布时间】:2013-11-18 19:44:22
【问题描述】:

您好,我一直在尝试使用 perl 进行 XML 操作(我尝试过 XML::LibXML、XML::Twig 和 XML::Simple),但我无法阅读。我想这可能是因为我的文件。 XML 文件示例:

<model-responses>
    <model mh="0x12a700">
      <attribute id="0x1006e">web2dda5p.sicredi.net</attribute>
      <attribute id="0x12d7f">172.25.76.18</attribute>
      <attribute id="0x110df">00:50:56:80:02:1e</attribute>
    </model>
<model mh="0x12a900">
  <attribute id="0x1006e">sicorr-apps14.sicredi.net</attribute>
  <attribute id="0x12d7f">172.19.33.17</attribute>
  <attribute id="0x110df">00:50:56:8a:02:21</attribute>
</model>

</model-responses>

我想把它变成:

<model-responses>
 <model>
    <name>web2dda5p.sicredi.net</name>
    <ip>172.25.76.18</ip>
    <mac>00:50:56:80:02:1e</mac>
    <modelhandle>0x12a700</modelhandle>
 </model>

我们将不胜感激。 提前谢谢。 恩里克·康多塔

【问题讨论】:

    标签: xml perl parsing


    【解决方案1】:
    use strict;
    use warnings;
    
    use XML::LibXML qw( );
    
    my $xml = <<'__EOI__';
    <model-responses>
        <model mh="0x12a700">
          <attribute id="0x1006e">web2dda5p.sicredi.net</attribute>
          <attribute id="0x12d7f">172.25.76.18</attribute>
          <attribute id="0x110df">00:50:56:80:02:1e</attribute>
        </model>
        <model mh="0x12a900">
          <attribute id="0x1006e">sicorr-apps14.sicredi.net</attribute>
          <attribute id="0x12d7f">172.19.33.17</attribute>
          <attribute id="0x110df">00:50:56:8a:02:21</attribute>
        </model>
    </model-responses>
    __EOI__
    

    my %attribute_name_from_id = (
       '0x1006e' => 'name',
       '0x12d7f' => 'ip',
       '0x110df' => 'mac',
    );
    
    my $parser = XML::LibXML->new();
    my $doc = $parser->parse_string($xml);
    
    for my $model ($doc->findnodes('/model-responses/model')) {
       my $handle = $model->getAttribute('mh');
       $model->removeAttribute('mh');
    
       for my $attribute ($model->getChildrenByTagName('attribute')) {
          my $attribute_id = $attribute->getAttribute('id');
          my $attribute_name = $attribute_name_from_id{$attribute_id}
             or next;
    
          my $ele = XML::LibXML::Element->new($attribute_name);
          $ele->appendChild($_) for $attribute->childNodes();
    
          $model->insertAfter($ele, $attribute);
          $model->removeChild($attribute);
       }
    
       {
          my $ele = XML::LibXML::Element->new('modelhandle');
          $ele->appendText($handle);
          $model->appendChild($ele);
       }
    }
    
    print $doc->toString();
    

    【讨论】:

      【解决方案2】:

      你可以使用XSLT parser

       use XML::XSLT;
      
       my $xslt = XML::XSLT->new ($xsl, warnings => 1);
      
       $xslt->transform ($xmlfile);
       print $xslt->toString;
      
       $xslt->dispose();
      

      XSLT

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
          <xsl:output method="xml" encoding="utf-8" indent="no"/>
      
          <xsl:template match="/">
              <model-responses>
                  <xsl:for-each select="/model-responses/model">
      
                      <model>
                          <name>
                              <xsl:value-of select="attribute[@id='0x1006e']"/>
                          </name>
                          <ip>
                              <xsl:value-of select="attribute[@id='0x12d7f']"/>
                          </ip>
                          <mac>
                              <xsl:value-of select="attribute[@id='0x110df']"/>
                          </mac>
                          <modelhandle>
                              <xsl:value-of select="@mh"/>
                          </modelhandle>
                      </model>
      
                  </xsl:for-each>
              </model-responses>
          </xsl:template>
      
      </xsl:stylesheet>
      

      你可以在这里建造 http://www.online-toolz.com/tools/xslt-transformation.php

      <?xml version="1.0" encoding="utf-8"?>
      <model-responses>
          <model>
              <name>web2dda5p.sicredi.net</name>
              <ip>172.25.76.18</ip>
              <mac>00:50:56:80:02:1e</mac>
              <modelhandle>0x12a700</modelhandle>
          </model>
          <model>
              <name>sicorr-apps14.sicredi.net</name>
              <ip>172.19.33.17</ip>
              <mac>00:50:56:8a:02:21</mac>
              <modelhandle>0x12a900</modelhandle>
          </model>
      </model-responses>
      

      编辑——也许更标准的 XSLT

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
          <xsl:output method="xml" encoding="utf-8" indent="no"/>
      
          <xsl:template match="model-responses">
              <model-responses>
                  <xsl:apply-templates/>
              </model-responses>
          </xsl:template>
      
          <xsl:template match="model">
              <model>
                  <xsl:apply-templates/>
      
                  <modelhandle><xsl:value-of select='@mh'/></modelhandle>
              </model>
          </xsl:template>
      
          <xsl:template match="attribute[@id='0x1006e']">
              <name><xsl:value-of select='.'/></name>
          </xsl:template>
      
          <xsl:template match="attribute[@id='0x12d7f']">
              <ip><xsl:value-of select='.'/></ip>
          </xsl:template>
      
          <xsl:template match="attribute[@id='0x110df']">
              <mac><xsl:value-of select='.'/></mac>
          </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

        猜你喜欢
        • 2013-10-18
        • 2011-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多