我使用XML::Twig
来解析Azure的list-blob
REST API的输出。
特别是,我希望识别和删除未提交的孤立blob,我不确定如何最好地有效地使用XML::Twig
来执行此操作。我甚至不知道从哪里开始。
最终我需要检索孤立blob的<Name>
元素。
响应中未提交的Blob
仅当响应时,响应中列出了未提交的blob 在URI上指定了 include = uncommittedblobs 参数。 响应中列出的未提交的blob不包括任何 以下要素:
Last-Modified Etag Content-Type Content-Encoding Content-Language Content-MD5 Cache-Control Metadata
因此,在下面的简化示例中,您可以看到名为“test”的孤立blob,因为<Blob></Blob>
块不包含任何上述元素。
<?xml version="1.0" encoding="utf-8"?>
<EnumerationResults ServiceEndpoint="https://my**account.blob.core.windows.net/"
ContainerName="testonly">
<Blobs>
<Blob>
<Name>test</Name>
<Properties>
<Content-Length>0</Content-Length>
<BlobType>BlockBlob</BlobType>
<LeaseStatus>unlocked</LeaseStatus>
<LeaseState>available</LeaseState>
</Properties>
</Blob>
</Blobs>
<NextMarker/>
</EnumerationResults>
更新:
实际上,我可能过于简单了。接受的答案似乎不适用于下面的内容,它会打印所有内容:
<?xml version="1.0" encoding="utf-8"?>
<EnumerationResults ServiceEndpoint="https://my**account.blob.core.windows.net/" ContainerName="testonly">
<Blobs>
<Blob>
<Name>data/users/docx</Name>
<Properties>
<Last-Modified>Wed, 10 May 2017 20:21:25 GMT</Last-Modified>
<Etag>0x8D497E221E7A5AF</Etag>
<Content-Length>125632</Content-Length>
<Content-Type>application/octet-stream</Content-Type>
<Content-Encoding/>
<Content-Language/>
<Content-MD5/>
<Cache-Control/>
<Content-Disposition/>
<BlobType>BlockBlob</BlobType>
<LeaseStatus>unlocked</LeaseStatus>
<LeaseState>available</LeaseState>
</Properties>
</Blob>
<Blob>
<Name>test</Name>
<Properties>
<Content-Length>0</Content-Length>
<BlobType>BlockBlob</BlobType>
<LeaseStatus>unlocked</LeaseStatus>
<LeaseState>available</LeaseState>
</Properties>
</Blob>
</Blobs>
<NextMarker/>
</EnumerationResults>
我的代码:
sub blob_parse {
my $blob = $_;
$blob->first_child($_) and return
for qw( Last-Modified Etag Content-Type Content-Encoding
Content-Language Content-MD5 Cache-Control Metadata);
say "orph: ".$blob->first_child('Name')->text;
}
sub parseAndDelete {
### ORPHAN
$twig_handlers = {'Blobs/Blob' => \&blob_parse};
$twig = new XML::Twig(twig_handlers=>$twig_handlers);
$twig->parse($message);
}
答案 0 :(得分:2)
没有理由使用XML::Twig
提供的回调系统,除非您的XML数据非常庞大并且相应的数据结构占用了太多内存,这对于在Internet消息中获取的数据不太可能< / p>
我会这样实现它
use strict;
use warnings;
use feature 'say';
use XML::Twig;
use List::Util 'none';
my @unwanted = qw/
Last-Modified Etag Content-Type Content-Encoding
Content-Language Content-MD5 Cache-Control Metadata
/;
my $twig = 'XML::Twig'->new;
$twig->parsefile('blob.xml');
for my $blob ( $twig->find_nodes('Blobs/Blob') ) {
if ( none { $blob->find_nodes("Properties/$_") } @unwanted ) {
say $_->text for $blob->find_nodes('Name');
}
}
test
如果您的XML实际上格式正确并且您的示例数据是错误的,那么打印所有Name
元素的文本内容很简单
我已经使用过这个数据
<?xml version="1.0" encoding="utf-8"?>
<EnumerationResults ServiceEndpoint="https://my**account.blob.core.windows.net/"
ContainerName="testonly">
<Blobs>
<Blob>
<Name>test</Name>
<Properties>
<Content-Length>0</Content-Length>
<BlobType>BlockBlob</BlobType>
<LeaseStatus>unlocked</LeaseStatus>
<LeaseState>available</LeaseState>
</Properties>
</Blob>
</Blobs>
<NextMarker/>
</EnumerationResults>
use strict;
use warnings 'all';
use feature 'say';
use XML::Twig;
my $t = XML::Twig->new;
$t->parsefile( 'blob.xml');
say $_->text for $t->find_nodes('Blobs/Blob/Name');
test
答案 1 :(得分:2)
只需为Blob
创建一个处理程序,如果存在任何元素则不执行任何操作,否则打印该名称。使用first_child
方法检查blob的内部结构。
#! /usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use XML::Twig;
my $xml = '...';
my $twig = 'XML::Twig'->new(twig_handlers => {
Blob => sub {
my $properties = $_->first_child('Properties');
$properties->first_child($_) and return
for qw( Last-Modified Etag Content-Type Content-Encoding
Content-Language Content-MD5 Cache-Control Metadata
);
say $_->first_child('Name')->text;
},
});
$twig->parse($xml);