读取没有任何XML模块的xml文件 [英] read xml file without any XML module

查看:78
本文介绍了读取没有任何XML模块的xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read a XML form using Perl but I can not use any XML modules like XML::Simple, XML::Parse.

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

It is a simple XML form which has some basic information and a MS Doc attachment. I want to read this XML and download this attached Doc file then print the XML information in the screen.

但是我不知道如何在没有XML模块的情况下完成此操作,听说可以使用 Data :: Dumper 来解析XML文件,但是我对这个模块不熟悉,所以不了解如何做.

But I don't know any way how I can do this without a XML module, I heard that XML file can be parse using Data::Dumper but I am not familiar with this module, so not getting how to do this.

如果没有XML模块,有什么方法可以帮助我吗?

Could you please help me on this if there is any way to do this without a XML modules?

示例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>

推荐答案

我想再次重申这是一个糟糕的想法.因为XML 看起来像纯文本-它不是 纯文本.而且,如果您这样处理,那么您将创建易碎,不可维护和不可支持的代码,因为有可能有人以有效方式更改XML格式,所以这很可能会中断一天的时间.

I'd like to re-iterate that this is a BAD IDEA. Because whilst XML looks like plain text - it's isn't plain text. And if you treat it as such, you are creating brittle, unmaintainable and unsupportable code, which may well break one day, because someone changes the XML format in a valid way.

我强烈建议您的第一个调用端口回到您的项目,并指出在没有XML解析器的情况下解析XML就像试图用锤子将螺丝钉入一块木头一样.这样做虽然可以,但是效果却很差,而且坦率地说,这完全没有必要,因为螺丝刀存在并且可以正确,轻松地完成工作,并且广泛使用.

I would strongly suggest that your first port of call is go back to your project, and point out how parsing XML without an XML parser is rather like trying to use a hammer to put screws into a piece of wood. In that it sort of works, but the results are rather shoddy, and frankly it's completely unnecessary because screwdrivers exist and they do the job properly, easily and are widely available.

例如

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

can you tell me how I can print the author, title and price for each book id for the above XML file with a XML module ?

#!/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";
}

但是:

鉴于您的非常具体样本,您 也许可以将其视为纯文本".在执行此操作之前,您应该指出您的项目负责人,这是一种冒险的方法-您正在用锤子拧螺丝-从而造成了持续的支持问题风险,这是简单解决的只需安装一些免费的开放源代码即可.

Given your very specific sample, you may be able to get away with treating it as 'plain text'. Before you do this, you should point out to your project lead that this is a risky approach - you're putting in screws with a hammer - and therefore creating ongoing risk of support problems, which is trivially resolved by just installing a bit of freely available, open source code.

我只是建议这个 AT ,因为我不得不处理可笑的,不合理的类似项目需求.

I am only suggesting this AT ALL because I've had to deal with ludicrously unreasonable similar project demands.

赞:

#!/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";
   }
}

现在,不能起作用的原因是您上面的示例,可以完全有效地格式化为:

Now, the reason this doesn't work is your sample above can be perfectly validly formatted as:

<?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>

这就是为什么您有这么多评论说使用解析器"的原因-从上面的摘录中,我给您提供的简单示例...仅适用于一个,而另一些则混乱.

This is why you have so many comments that say 'use a parser' - from those snippets above, the simplistic example I gave you... will only work on one and break messily on the others.

但是XML::Twig解决方案可以正确处理所有问题. XML::Twig在CPAN上免费提供. (也有其他图书馆也做这项工作).而且它还预先打包了许多操作系统的默认"存储库.

But the XML::Twig solution handles them all correctly. XML::Twig is freely available on CPAN. (There's other libraries that do the job too just as well). And it's also pre-packaged with a lot of operating systems 'default' repositories.

这篇关于读取没有任何XML模块的xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆