我想重申这是一个坏主意。因为虽然 XML 看起来 像纯文本 - 它是 不是 纯文本。如果你这样对待它,你正在创建脆弱、不可维护和不可支持的代码,这很可能有一天会崩溃,因为有人以有效的方式更改了 XML 格式。
我强烈建议您首先访问您的项目,并指出在没有 XML 解析器的情况下解析 XML 就像尝试使用锤子将螺丝钉入木头一样。在这方面它有点工作,但结果相当粗制滥造,坦率地说,这是完全没有必要的,因为存在螺丝刀,它们可以正确、轻松地完成工作,并且可以广泛使用。
例如
您能告诉我如何使用 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";
}
但是:
鉴于您的非常具体示例,您可能能够将其视为“纯文本”而侥幸逃脱。在您执行此操作之前,您应该向您的项目负责人指出这是一种冒险的方法 - 您正在用锤子拧螺丝 - 因此会产生持续存在的支持问题风险,而这微不足道已解决只需安装一些免费可用的开源代码。
我只是建议这样做根本,因为我不得不处理可笑不合理的类似项目需求。
像这样:
#!/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>
这就是为什么你有这么多 cmets 说“使用解析器”的原因 - 从上面的那些 sn-ps 中,我给你的简单示例......只会在一个上工作,而在其他方面会混乱。
但是XML::Twig 解决方案可以正确处理它们。 XML::Twig 在 CPAN 上免费提供。 (还有其他库也可以完成这项工作)。它还预装了许多操作系统的“默认”存储库。