【问题标题】:Extracting only the values of xml removing the xml tag仅提取 xml 的值删除 xml 标记
【发布时间】:2012-10-26 11:07:03
【问题描述】:

例如,我有一个 xml 文件,

<title> hello <name> hi </name> <street> id </street> this is xml file </title>

这里的父节点是title。我将提取父节点内的文本,删除内部标签。

我已经尝试过使用正则表达式。但是除了使用正则表达式之外,还有什么方法可以使用一些基于 xml 的函数来删除标签。注意:标签名称事先不知道。

你好,我试过了,我用的是同一个xml

use XML::Simple; 
use Data::Dumper; 

my $simple = XML::Simple->new(); 
my $data = $simple->XMLin('XMLRemoval.xml'); 
my %oldHash = %$data; my %newHash = (); 

while ( my ($key, $innerRef) = each %oldHash ) 
{ 
    $newHash{$key} = @$innerRef[1]; 
} 

foreach $key ( keys %newHash ) 
{ 
    print $newHash{$key}; 
}

我得到了错误:不能使用字符串(“id”)作为 ARRAY ref 而“strict refs”

【问题讨论】:

  • 你检查过 XML::Simple 模块了吗??我想你会得到你想要的。
  • 我已经搜索过,但找不到任何可以使用 XML::Simple 删除的内容。你能告诉你是否知道方法吗
  • 如果你对哈希有一定的了解,并且通过使用 XMLin 方法你可以得到你想要的。再次,请从您的角度尝试,粘贴您的代码(错误,努力),以便我们提供帮助。要求代码并不好,因为这里的 ppl 是为了寻求帮助而不是为了完成您的任务。这也可能会导致投反对票。
  • 你好我试过这个,我用同样的xml使用XML::Simple;使用 Data::Dumper;我的 $key;我的 $simple = XML::Simple->new();我的 $data = $simple->XMLin('XMLRemoval.xml');我的 %oldHash = %$data;我的 %newHash = ();而 ( 我的 ($key, $innerRef) = 每个 %oldHash ) { $newHash{$key} = @$innerRef[1]; } foreach $key (keys %newHash) { print $newHash{$key};我得到了错误:Can't use string ("id ") as an ARRAY ref while "strict refs"

标签: xml perl xml-parsing extract


【解决方案1】:

根据您的要求,您可以试试这个。 我在示例中使用了您提供的文件。

我们在这里定义 XML 中的根键内容(或重命名),您需要选择一个不在您的 XML 中的键(我选择了根内容)。

#!/usr/bin/perl
use strict;
use XML::Simple;
use Data::Dumper;
my $key;
my $simple = XML::Simple->new();
my $data = $simple->XMLin('XMLRemoval.xml', 'ContentKey' => 'root-contents');
print Dumper $data;
my $val = $data->{'root-contents'};
if(ref($val) =~ /Array/i)
{
    foreach (@$val)
    {
        print "$_\n";
    }
}
else
{
    print "$val\n";
}

请阅读 XML::Simple 文档,有很多选项可以根据您的要求进行调整。

我会把调试部分留给你,让你的代码检查错误是什么以及如何解决它(这本身就是解释性的):)。

【讨论】:

  • 嗨,代码工作正常。我还有一个疑问。我的 $path = "D:\DocRepos\Tasks\PerlPrograms\XMLRemoval.xml";我的 $data = $simple->XMLin($path, 'ContentKey' => 'root-contents');当我尝试将路径分配给变量时出现错误。文件不存在:D:DocReposTasksPerlProgramsXMLRemoval.xml。请帮帮我。
  • 在你的代码中你有'\',你需要使用另一个'\'来转义它。否则,您可以将路径括在单引号中。请阅读有关它的 Perl 文档。这个错误很简单,你应该能够解决它。
  • 我不会不尝试就问问题。我已经尝试了上述情况,但仍然无法正常工作。
  • 你确定它不起作用吗?你有什么错误?你有没有像这样使用它:$path="D:\\DocRepos\\Tasks\\PerlPrograms\\XMLRemoval.xml" ??
【解决方案2】:
use strict;
use warnings;

use features qw/say/;
use Mojo::DOM;

my $dom = Mojo::DOM->new('<title> hello <name> hi </name> <street> id </street> this is xml file </title>');

say $dom->all_text;
# hello hi id this is xml file

say $dom->at('title')->all_text;
# hello

你懂的

【讨论】:

    【解决方案3】:

    你可以使用XML::XSH2:

    open file.xml ;
    echo (/title) ;       # hello  hi   id  this is xml file
    echo /title/text() ;  # hello     this is xml file 
    

    【讨论】:

    【解决方案4】:

    最残忍的方式是:

    use strict;
    use warnings;
    
    use feature 'say';
    
    
    my $text = '<title> hello <name> hi </name> <street> id </street> this is xml file </title>' ;
    
    $text =~ s|<.+?>||g;
    say "Text |$text|";
    

    但是,您可能知道,is not ok to parse html with regex

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 2014-12-31
      相关资源
      最近更新 更多