- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试运行一个 perl 脚本,其中包含一些正在构建的 Twig 。此脚本应获取 xml 文件并返回文件中作为属性存在的版本号。每次我尝试解析一个大文件 (23 MB) 时,脚本都会崩溃并显示以下内容 -
"Child 341 terminated with signal 11".
调用将获得所需属性的子例程的代码-
my $version = $strm_obj->get_attr(file=>$file1,tag=>"config",attr=>"contentversion");
print "Version of $file1 is $version \n";
my $globalversion = $strm_obj->get_attr(file=>$file2,tag=>"config",attr=>"globalcontentversion");
print "Version of $file2 is $globalversion \n";
获取所需属性的子程序 -
sub get_attr{
my ($self,%args) = @_;
my $file = $args{file};
my $tag = $args{tag};
my $attr = $args{attr};
my $val;
$self->{_ATTR} = $attr;
$self->{_TAG} = $tag;
test_log(DEBUG,"Value of tag is $tag, attribute is $attr");
my $twig= XML::Twig->new(
twig_roots => { $tag
=> sub {$self->get_attr_helper(@_,$tag,\$val); } } )
->parsefile($file);
if ($val){
test_log(INFO,"value of attribute $attr is $val");
}
if (!$val){
test_log(INFO,"The attribute $attr that you are looking for, is not present in $file");
return -1;
}
$twig->purge;
$twig->dispose;
return $val;
}
sub get_attr_helper{
my($self,$obj,$tag,$act_tag,$val) = @_;
my $attr = $self->{_ATTR};
#print "my attr is $attr\n";
for my $node ($tag->findnodes("//$self->{_TAG}")){
if ($node->att("$attr")){
$$val = $node->att("$attr");
}
}
$obj->purge;
}
xml文件格式如下:
$文件1 -
<config contentversion="378">
<tag1>
.
.
.
<tag n>
</config>
$文件2-
<config globalcontentversion="378">
<tag1>
.
.
.
<tag n>
</config>
我真的不能在这里提供实际的 xml 文件。
我知道这个脚本最多占用我机器大约 20% 的内存(2GB RAM)。
我环顾四周,一直找不到解决办法。
如何消除段错误?
最佳答案
很难给出具体的答案,因为段错误意味着某些东西乱七八糟地坏了(这是一个基于内存的问题)。
XML 很容易占用大量内存,在很大程度上,XML::Twig 的最大优势之一是它能够使用 twig_handlers
和 进行解析和丢弃清除
。
这使得它非常适合从 XML 中部分提取内容。
我看不出具体是什么导致了段错误,但是 - 在 perl 中,您不会经常遇到段错误,这很可能是外部原因。
除此之外 - 您似乎正在做一些非常复杂的事情来从您的文件中提取版本号。 (这是假设我没有误读您要提取的内容)。
这样的东西不符合您的需求吗?:
use strict;
use warnings;
use XML::Twig;
sub get_attr {
my ( $self, %args ) = @_;
my $file = $args{file};
my $tag = $args{tag};
my $attr = $args{attr};
my $twig = XML::Twig->new()->parsefile($file);
my $val = $twig->root->first_child($tag)->att($attr);
#maybe error check to see if 'first_child($tag)' is defined first?
return $val;
}
尽管如果您的“doc root”始终是您要提取的“config”分支,您可以进一步简化:
my $val = $twig->root->att($attr);
我试过了 - 它适用于您目前提供的两个样本。如果您仍然出现段错误,我会考虑检查您安装的内容。
(可能值得采用“ Twig 处理程序”方法来捕获标签,但我认为这不是特别必要,因为最大的优势是可以边走边清除,这似乎没有必要,因为问题的大小)。
XML::Twig 中列出了一个错误:
http://search.cpan.org/~mirod/XML-Twig-3.48/Twig.pm#BUGS
segfault during parsing This happens when parsing huge documents, or lots of small ones, with a version of Perl before 5.16.
This is due to a bug in the way weak references are handled in Perl itself.
The fix is either to upgrade to Perl 5.16 or later (perlbrew is a great tool to manage several installations of perl on the same machine).
An other, NOT RECOMMENDED, way of fixing the problem, is to switch off weak references by writing XML::Twig::_set_weakrefs( 0); at the top of the code. This is totally unsupported, and may lead to other problems though,
虽然我不太确定这是否适用于您,因为我真的不会将“23MB”称为巨大的 XML。 (请记住,XML 的内存占用量约为 10 倍)。
关于xml - 带有 XML Twig 段错误的 Perl 脚本,子进程以信号 11 终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20894222/
如果我终止应用程序,我在尝试保持我的功能运行时卡住了。 是否可以在应用程序未运行时保持核心位置(地理围栏/地理定位)和核心蓝牙运行?如果可能如何解决我的问题?我已经检查了背景模式,并实现了核心定位方法
该程序要求用户输入一个数字,然后从列表中返回详细信息。我该怎么做? do { Scanner in = new Scanner(System.in);
我正在开发一个内部分发的 iOS 应用程序(即,没有应用程序商店),我希望能够以恒定的 10 分钟间隔报告设备的位置。 无论如何,我在我的 plist 中包含了 location 作为字段 UIBac
我的 mongodb 服务器突然收到信号 15(终止)。我不知道为什么 mongodb 崩溃了。以下是日志消息。 Mon Jun 27 07:33:31.701 [signalProcessingTh
我按顺序运行了一堆malloc,并且每次都检查以确保它是成功的。像这样: typedef struct { int *aray; char *string; } mystruct; m
这个问题已经有答案了: How to stop a running pthread thread? (4 个回答) 已关闭 8 年前。 可以使用 pthread_join() 停止线程。但让我们想象一
#include #include #include struct node{ char data; int p; struct node *ptr; }; struct node *st
这个问题已经有答案了: Why should I use a semicolon after every function in javascript? (9 个回答) 已关闭 8 年前。 好吧,我问
我有一个启动多个工作线程的函数。每个工作线程都由一个对象封装,该对象的析构函数将尝试加入线程,即调用if (thrd_.joinable()) thrd_.join();。但是,每个 worker 必
我正在实现一个应用程序,当用户摇动手机时,该应用程序会监听并采取行动。 所以我实现了以下服务: public class ShakeMonitorService extends Service {
我在使用 Xcode 时遇到问题,其中弹出错误“Source Kit Service Terminated”,并且所有语法突出显示和代码完成在 Swift 中都消失了。我怎样才能解决这个问题? 这是一
我想为我的控制台应用程序安全退出,该应用程序将使用单声道在 linux 上运行,但我找不到解决方案来检测信号是否发送到它或用户是否按下了 ctrl+c。 在 Windows 上有内核函数 SetCon
关键: pthread_cancel函数发送终止信号pthread_setcancelstate函数设置终止方式pthread_testcancel函数取消线程(另一功能是:设置取消点) 1 线程取消
下面的程序在不同的选项级别下有不同的行为。当我用 -O3 编译它时,它永远不会终止。当我用 -O0 编译它时,它总是很快就会终止。 #include #include void *f(void *
我有 3 个节点的 K8S 集群,我创建了 3 个副本 pod,应用程序 app1 在所有 pod 上运行,我通过运行 service yaml 文件建立了服务,我可以看到通过运行 kubectl g
我打算使用 nginx 来代理 websocket。在执行 nginx reload/HUP 时,我知道 nginx 等待旧的工作进程停止处理所有请求。然而,在 websocket 连接中,这可能不会
在 Ubuntu 9.10 上使用 PVM 3.4.5-12(使用 apt-get 时的 PVM 包) 添加主机后程序终止。 laptop> pvm pvm> add bowtie-slave add
我编写了一个应用程序来从 iPhone 录制视频。它工作正常,但有一个大问题。当 AVCaptureSession 开始运行并且用户尝试从其库(iPod)播放音频时。此操作将使 AVCaptureSe
我将如何使用NSRunningApplication?我有与启动应用程序相反的东西: [[NSWorkspace sharedWorkspace] launchApplication:appName]
我正在使用 NSTask 执行一系列长时间运行的命令,如下所示: commandToRun = @"command 1;command2"; NSArray *arguments = [NSArray
我是一名优秀的程序员,十分优秀!