读取没有任何XML模块的xml文件

时间:2015-07-27 10:56:02

标签: xml perl xml-parsing perl-module

我正在尝试使用Perl读取XML表单,但我不能使用任何XML模块,如XML :: Simple,XML :: Parse。

这是一个简单的XML表单,它包含一些基本信息和MS Doc附件。 我想阅读这个XML并下载这个附加的Doc文件,然后在屏幕上打印XML信息。

但我不知道如何在没有XML模块的情况下执行此操作,我听说可以使用 Data :: Dumper 解析XML文件但是我不熟悉这个模块,所以不知道怎么做。

如果没有XML模块有任何办法可以帮助我吗?

示例XML:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

3 个答案:

答案 0 :(得分:5)

我想重新认识一下,这是一个不好的想法。因为XML 看起来像一样纯文本 - 它的不是纯文本。如果你这样对待它,你就会创建脆弱,不可维护和不受支持的代码,这可能有一天会破坏,因为有人会以有效的方式更改XML格式。

我强烈建议您的第一个调用端口返回到您的项目,并指出如何在没有XML解析器的情况下解析XML就像尝试使用锤子将螺钉放入一块木头中一样。因为它有点工作,但结果是相当粗制滥调,坦率地说,它完全没有必要,因为螺丝刀存在并且它们能够正常,轻松地完成工作并且可以广泛使用。

E.g。

  

您能告诉我如何使用XML模块打印上述XML文件的每个书籍ID的作者,标题和价格吗?

#!/usr/bin/env perl
use strict;
use warnings;

use XML::Twig;
my $twig = XML::Twig -> new -> parsefile ( 'your_file.xml' );
foreach my $book ( $twig -> get_xpath ( '//book' ) ) {
    print join ("\n", 
         $book -> att('id'),
         $book -> field('author'),
         $book -> field('title'),
         $book -> field('price'), ),"\n----\n";
}

然而:

鉴于您的非常具体的示例,您可以能够将其视为纯文本&#39;。在你这样做之前,你应该向你的项目负责人指出这是一个冒险的方法 - 你用锤子装螺丝 - 因此造成持续的支持问题风险,这是琐事通过安装一些免费提供的开源代码解决。

我只是建议 AT ALL ,因为我必须处理荒谬的不合理的类似项目要求。

像这样:

#!/usr/bin/env perl
use strict;
use warnings;

while ( <> ) {
   if ( m/<book/ ) { 
       my ( $id ) = ( m/id="(\w+)"/ ); 
       print $id,"\n";
   }
   if ( m/<author/ ) { 
        my ( $author ) = ( m/>(.*)</ );
        print $author,"\n";
   }
}

现在,无法工作的原因是您上面的示例可以完全有效地格式化为:

<?xml version="1.0"?>
<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications 
      with XML.</description></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description></book></catalog>

或者

<?xml version="1.0"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>An in-depth look at creating applications 
      with XML.</description>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
  </book>
</catalog>

或者:

<?xml version="1.0"?>
<catalog
><book
id="bk101"
><author
>Gambardella, Matthew</author><title
>XML Developer's Guide</title><genre
>Computer</genre><price
>44.95</price><publish_date
>2000-10-01</publish_date><description
>An in-depth look at creating applications 
      with XML.</description></book><book
id="bk102"
><author
>Ralls, Kim</author><title
>Midnight Rain</title><genre
>Fantasy</genre><price
>5.95</price><publish_date
>2000-12-16</publish_date><description
>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description></book></catalog>

或者:

<?xml version="1.0"?>

<catalog>
  <book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications 
      with XML.</description></book>
  <book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description></book>
</catalog>

这就是为什么你有这么多评论说'使用解析器&#39; - 从上面的那些片段中,我给你的简单例子......只会在一个片段上工作,而在其他片段上乱七八糟。

XML::Twig解决方案正确处理它们。 CPAN上免费提供XML::Twig。 (还有其他图书馆也可以完成这项工作)。而且它还预先打包了很多操作系统,默认情况下&#39;库。

答案 1 :(得分:2)

嗯,XML解析器只是代码。 CPAN模块都是开源的,所以我想你可以将代码从an XML parsing module from CPAN复制到你的程序中。

但实际上,这是一个令人难以置信的愚蠢想法。你为什么不用这个模块?你可以花更多的时间在使用已移除的模块上获得好处。许多现代Perl Perl编程包括从CPAN安装正确的模块并将它们连接在一起。如果您不使用CPAN模块,那么您将从Perl的大部分功率中剔除自己。

如果你真的不能解除这个限制,那么(认真地)会得到更好的雇主。

答案 2 :(得分:0)

如果您不能使用任何模块,那么您应该查看像XML::LibXML这样的模块的源代码,并了解它们如何处理XML然后按照您的方式实现它,但不建议这样做。

请参阅:Perl for XML Processing