【问题标题】:How can I use XML::Simple to configure HTML::FormFu?如何使用 XML::Simple 配置 HTML::FormFu?
【发布时间】:2009-12-19 23:58:08
【问题描述】:

我假设您可以将 XML::Simple 与 HTML::FormFu 一起使用,因为 FromFu 使用 Config::Any 来加载它的配置数据。

但是,我似乎找不到任何与 HTML::FormFu 一起使用的示例 xml 配置。不仅我得到一个错误。我不确定我的 xml 结构是否正确以创建所需的表单。例如,在选项上,formfu 想要一个数组引用数组。但我很确定这个 xml 会产生一个哈希引用数组。

我做的不对...这是我的 xml 文件的开头:

<?xml version="1.0" encoding="utf-8" ?>
<config>
  <indicator>submit</indicator>
  <elements>
    <element type="FieldSet" name="overrides" label="Over Rides">
     <attributes title="Use these fields to override the csv value with this constant value" />
      <elements>
       <element type="text" name="client" label="Client" />
       <element type="Select" name="bid_type" label="Bid Type">
      <options bid="Bid" />
      <options approved="Approved" />
    </element>
    <element type="text" name="client_pay" label="Client Pay" />
    <element type="text" name="due_date" label="Due Date" />
    <element type="text" name="start_date" label="Start Date" />
    <element type="Radiogroup" name="category" label="Category">
      <options grass_cut_initial="Grass Cut - Initial"/>
      <options grass_cut_recut="Grass Cut - Recut"/>
      <options secure="Secure"/>
      <options winterization="Winterization"/>
      <options rehab="Rehab" />
      <options custom="Custom"/>
    </element>
    <element type="text" name="contractor" label="Contractor" />
    <element type="text" name="contractor_pay" label="Contractor Pay" />
  </elements>
</element>

我收到此错误:

[调试] Catalyst::Controller::HTML::FormFu::Action::FormConfig 加载配置文件 'workorders/import' [错误] 在 myapsjobs::Controller::WorkOrders->import 中捕获异常“解析/home/jon/aps-dev/myapsjobs/root/forms/workorders/import.xml 时出错:/home/jon/aps-dev/myapsjobs /root/forms/workorders/import.xml:38:解析器错误:标记配置行 1 中的数据过早结束 在 /usr/local/share/perl/5.10.0/HTML/FormFu/ObjectUtil.pm 第 502 行"

【问题讨论】:

  • 您是否有某些特殊原因要使用 XML?例如,您是否通过其他工具动态创建配置?

标签: xml perl catalyst xml-simple html-formfu


【解决方案1】:

尝试创建一个XML::Simple 将解析为特定数据结构的 XML 文件可能会非常痛苦。我发现处理这个问题的最简单方法是从您想要的数据结构开始,通过XMLout 运行它,然后根据您认为合适的方式修改生成的 XML。

use strict;
use warnings;
use XML::Simple;

my $config = { 
    'indicator' => 'edit',
    'elements' => [
        {   
            'name' => 'overrides',
            'label' => 'Over Rides',
            'type' => 'Fieldset',
            'attributes' => {
                'title' => 'Use these fields to override the csv value with this constant value',
            },
            'elements' => [
                {   
                    'type' => 'text',
                    'name' => 'client',
                    'label' => 'Client',
                },  
                {
                    'type' => 'Select',
                    'name' => 'bidy_type',
                    'label' => 'Bid Type',
                    'options' => [
                        [ 'bid' => 'Bid' ],
                        [ 'approved' => 'Approved' ],
                    ],
                },
                {
                    'type' => 'text',
                    'name' => 'client_pay',
                    'label' => 'Client Pay',
                },
                {
                    'type' => 'text',
                    'name' => 'due_date',
                    'label' => 'Due Date',
                },
                {
                    'type' => 'text',
                    'name' => 'start_date',
                    'label' => 'Start Date',
                },
                {
                    'type' => 'Radiogroup',
                    'name' => 'category',
                    'label' => 'Category',
                    'options' => [
                        [ 'grass_cut_initial' => 'Grass Cut - Initial' ],
                        [ 'grass_cut_recut' => 'Grass Cut - Recut' ],
                        [ 'secure' => 'Secure' ],
                        [ 'winterization' => 'Winterization' ],
                        [ 'rehab' => 'Rehab' ],
                        [ 'custom' => 'Custom' ],
                    ],
                },
                {
                    'type' => 'text',
                    'name' => 'contractor',
                    'label' => 'Contractor',
                },
                {
                    'type' => 'text',
                    'name' => 'contractor_pay',
                    'label' => 'Contractor Pay',
                },
            ],
        },
    ],
};
my $xml = XMLout($config, 'KeyAttr' => []);

print "$xml\n";

结果

<opt indicator="edit">
  <elements label="Over Rides" name="overrides" type="Fieldset">
    <attributes title="Use these fields to override the csv value with this constant value" />
    <elements label="Client" name="client" type="text" />
    <elements label="Bid Type" name="bidy_type" type="Select">
      <options>
        <anon>bid</anon>
        <anon>Bid</anon>
      </options>
      <options>
        <anon>approved</anon>
        <anon>Approved</anon>
      </options>
    </elements>
    <elements label="Client Pay" name="client_pay" type="text" />
    <elements label="Due Date" name="due_date" type="text" />
    <elements label="Start Date" name="start_date" type="text" />
    <elements label="Category" name="category" type="Radiogroup">
      <options>
        <anon>grass_cut_initial</anon>
        <anon>Grass Cut - Initial</anon>
      </options>
      <options>
        <anon>grass_cut_recut</anon>
        <anon>Grass Cut - Recut</anon>
      </options>
      <options>
        <anon>secure</anon>
        <anon>Secure</anon>
      </options>
      <options>
        <anon>winterization</anon>
        <anon>Winterization</anon>
      </options>
      <options>
        <anon>rehab</anon>
        <anon>Rehab</anon>
      </options>
      <options>
        <anon>custom</anon>
        <anon>Custom</anon>
      </options>
    </elements>
    <elements label="Contractor" name="contractor" type="text" />
    <elements label="Contractor Pay" name="contractor_pay" type="text" />
  </elements>
</opt>

不完全是您所期望的 XML,但它可以完成工作。您还可以通过XMLin 运行它并检查生成的数据结构来仔细检查它是否有效:

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

my $xml = '...';
my $config = XMLin($xml, 'KeyAttr' => []);
print Dumper($config);

我之所以使用KeyAttr 选项是因为这个caveat

如果您希望“往返”任意 从 Perl 到 XML 的数据结构和 回到 Perl,那么你可能应该 禁用数组折叠(使用 KeyAttr 选项)都带有 XMLout() 和 使用 XMLin()。

另外,我似乎找不到将选项传递给Config::Anyload_config_file 的方法(不过,我没有花太多时间在文档中寻找HTML::FormFu)。这意味着您可能必须自己使用XML::Simple 才能将数据结构传递给populate


如您所见,在使用 HTML::FormFu 时,XML 配置文件确实不是最简单的方法。如果您对其他方法持开放态度,我建议使用对 Perl 数据结构有更好映射的东西,例如 YAML(这可能是它在 documentation examples 中使用的原因之一)。就个人而言,我只是使用 Perl 创建表单并将代码/配置粘贴到模块中。

【讨论】:

    猜你喜欢
    • 2012-02-21
    • 2022-01-06
    • 2013-01-10
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    相关资源
    最近更新 更多