【问题标题】:Reformatting XML document in Perl?在 Perl 中重新格式化 XML 文档?
【发布时间】:2013-11-12 00:23:14
【问题描述】:

我有一个格式不正确的 XML 文档,例如一行会有很多元素:

<ModelRefs><ParentViewName>schematic</ParentViewName><ParentCellName>NCH_HS1_MV5</ParentCellName><InstanceName>DSDP</InstanceName><InstanceCellName>DIODE2MI_perim_shrink</InstanceCellName><InstanceLibName>tiRefLib</InstanceLibName></ModelRefs>

我想重新格式化它——我尝试在 XML::LibXML 库中读取/解析它,但无论我在(1、2 或 3 等)中使用哪种格式选项 $xmldoc->toFile($file, 2);

我仍然没有将它“标准化”并正确缩进......你能帮我怎么做吗? (我的机器上没有 Twig)...

【问题讨论】:

  • 对于带有空白的文档,您需要no_blanks =&gt; 1,但对于您提供的,它不会有任何区别。

标签: xml perl xml-parsing libxml2


【解决方案1】:

我知道你说你没有可用的XML::Twig,但这确实很好。

use strict;
use warnings;
use XML::Twig;

my $twig = XML::Twig -> new ( 'pretty_print' => 'indented' ) -> parse ( \*DATA ) -> print;


__DATA__

<ModelRefs><ParentViewName>schematic</ParentViewName><ParentCellName>NCH_HS1_MV5</ParentCellName><InstanceName>DSDP</InstanceName><InstanceCellName>DIODE2MI_perim_shrink</InstanceCellName><InstanceLibName>tiRefLib</InstanceLibName></ModelRefs>

【讨论】:

    猜你喜欢
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2012-01-22
    • 2011-02-10
    • 2010-09-09
    • 1970-01-01
    相关资源
    最近更新 更多