我尝试用XML::Simple和XML::Twig解析一个XML文件,结果是相同的。文件中的其他字段工作得很好。
有关的文件可以在这里检索到:
卷曲-s "http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=2.16.840.1.113883.6.103&mainSearchCriteria.v.c=130“
Is this a problem with the parser or the file? The output was the same with both parsers. The HTML-tags in the string is stored in the XML 输入字段(在名为“汇总”的xml标记中):
Output after XML-parsing:Solution to the problem: The XML files contains a carriage return "“这会给解析器带来问题。在我下载XML文件之后,我用以下行删除了回车:
sed -i 's/
//g' *.xml解析器现在按预期工作。
更新:回车不影响解析器,只影响出现截断和混淆的输出。然而,移除它确实解决了我的问题。
发布于 2011-06-06 11:42:10
当将curl解析为管道时,我确实得到了一些奇怪的结果(使用XML::Twig->new->parse( curl -s "http://..." |):内容似乎被截断,从调用更改到调用.
如果我解析从curl结果或XML::Twig的本机parseurl方法创建的文件,那么结果是常量的,并且是您想要的:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig->new->parseurl( "http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=2.16.840.1.113883.6.103&mainSearchCriteria.v.c=130" );
my $summary = $twig->first_elt( 'summary');
print $summary->text, "\n";老实说,我不知道为什么会这样。我将尝试更多地研究它,但我怀疑我无能为力:如果问题出现在XML::Simple和XML::Twig中,那么它可能在堆栈的较低级别,XML::解析器或expat,以及它们与curl的交互。
https://stackoverflow.com/questions/6250726
复制相似问题