Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Perl从XML配置文件生成HTML web表单,反之亦然_Html_Xml_Perl_Perl Module - Fatal编程技术网

使用Perl从XML配置文件生成HTML web表单,反之亦然

使用Perl从XML配置文件生成HTML web表单,反之亦然,html,xml,perl,perl-module,Html,Xml,Perl,Perl Module,我在以下问题上需要帮助。有一个我共同编写并目前管理的应用程序,它是用C和Haskell的组合编写的。应用程序可以通过单个XML文件进行定制和配置。它是一个没有用户界面的后端应用程序。我们被要求通过web界面为该应用程序提供图形界面。每个配置选项都是html表单中的一个表单字段,如下所示 configuration1 string1 configuration2 string2 etc etc 图形前端必须是将用户输入的数据转换为包含应用程序设置的XML文件的web表单。当用户保存表单时,更

我在以下问题上需要帮助。有一个我共同编写并目前管理的应用程序,它是用C和Haskell的组合编写的。应用程序可以通过单个XML文件进行定制和配置。它是一个没有用户界面的后端应用程序。我们被要求通过web界面为该应用程序提供图形界面。每个配置选项都是html表单中的一个表单字段,如下所示

configuration1  string1
configuration2  string2
etc
etc
图形前端必须是将用户输入的数据转换为包含应用程序设置的XML文件的web表单。当用户保存表单时,更改将写入XML文件。当用户打开表单时,XML文件中的配置将显示在HTML表单字段中

我们的团队只处理纯后端的东西,我们对GUI之类的东西一无所知。我们的限制是前端必须用Perl编写,并使用LibXML模块以符合公司标准。我们的团队纯粹是C和Haskell,这是我们第一次收到这样的请求。如果您能提供任何帮助,我将不胜感激。如果您能尽可能详细地包含代码示例,这将是一个很大的帮助。我们对Perl知之甚少,但通过一个足够清晰的示例,我们可以做到这一点。通常处理这类事情的团队正在重组,我们等不及了,因为我们需要尽快建立这个接口


多谢各位

不要自己做这件事。您不知道Perl或web应用程序,您提到的关于libxml的限制可能只是其中之一。每一个微小的配置和输入错误都会花费你数小时或数天的时间来解决。你最终会感到痛苦和压力,用户也会如此

您的web团队中的某个人可以在一天内生成一个带有支持测试和文档的简单表单。然后,您可以扩展和修复从工作应用程序开始的知识中的安全漏洞


如果你不能在公司内部找到人,你可以雇佣一个编码员,但要尽你所能先在公司内部完成。不要自己这样做。

对OP说:如果您没有正确描述配置格式,XML片段会对我们有所帮助。实际上,您可能需要修改下面的解决方案以匹配您的格式

我采用了以下格式:

<conf>
  <option><name>configuration1</name><value>string1 modified</value></option>
  <option><name>configuration2</name><value>string2 re-modded</value></option>
  <option><name>configuration3</name><value>string3</value></option>
</conf>

配置1字符串1已修改
重新修改的配置2字符串2
配置3串3
处理此格式的代码为:

#!/usr/bin/perl 

use strict;
use warnings;

use CGI qw( -nosticky -utf8 :standard form );
use XML::LibXML;

my $CONF="/web/data/conf.xml"; # or wherever your XML is located

# dispatch table, action => code to process it
my $dispatch= { display => \&display_conf,
                edit    => \&display_edit_form,
                save    => \&save_edits,
              };

# action is "what to do"
my $action= param( 'action') || 'display';

$dispatch->{$action}->() || exit_error( "wrong action");
exit;

# load the XML and build a table to display it
sub display_conf
  { my $conf= XML::LibXML->load_xml( location => $CONF);
    my $content;
    foreach my $option ($conf->findnodes( '/conf/option'))
      { $content .= Tr( td( $option->findvalue( './name')), 
                        td( $option->findvalue( './value'))
                      );
      }
    $content= table( $content);
    # add a link to the edit form
    $content .= p( a({ href => url() . '?action=edit' }, 'edit')); 
    output( $content);                
  }

# load the XML and build a form that will let you edit it
sub display_edit_form
  { my $conf= XML::LibXML->load_xml( location => $CONF);
    my $content;
    foreach my $option ($conf->findnodes( '/conf/option'))
      { $content .= Tr( td( $option->findvalue( './name')), 
                        td( input( { type => "text", size => 40, 
                                     name => $option->findvalue( 'name'), 
                                     value => $option->findvalue( './value')}
                                 )
                          )
                      );
      }
    $content= table( $content);
    $content= form( { action => url() }, 
                    hidden( { name => 'action', value => 'save', override => 1 }),
                    $content,
                    submit( 'Save'),
                  );
    output( $content);                
  }

# load the XML, go through all options, update the value from the form, 
# save the XML, display it value, from the form
sub save_edits
  { my $conf= XML::LibXML->load_xml( location => $CONF);
    foreach my $option ($conf->findnodes( '/conf/option'))
      { my $new_value= param( $option->findvalue( './name'));
        my( $value_node)= $option->findnodes( './value');
        $value_node->removeChildNodes();
        $value_node->appendText( $new_value);
      }
    $conf->toFile( $CONF);
    display_conf();
  }

# placeholder, 
sub exit_error
  { my $message= shift;
    print header(), p($message);
  }

# output subs, load the template, inject the content and output (with header)   

sub output
  { my $content= shift;
    print header(), fill_in_string( template(), content => $content );
  }


sub fill_in_string
  { my( $template, %param)= @_;
    $template=~ s{<<(\w+)>>}{$param{$1}}g;
    return $template;
  }

sub template
  { return join '', <DATA>; }

# template, very simple
__DATA__
<html>
  <head>
    <title>Configuration Editor</title>
  </head>
  <body>
    <h1>Configuration Editor</h1>
    <h2>Configuration</h2>
      <<content>>
    </h2>
  </body>
</html>
#/usr/bin/perl
严格使用;
使用警告;
使用CGI qw(-nosticky-utf8:标准格式);
使用XML::LibXML;
my$CONF=“/web/data/CONF.xml”#或者XML所在的任何位置
#调度表,action=>处理它的代码
my$dispatch={display=>\&display\u conf,
编辑=>\&显示编辑表单,
保存=>\&保存编辑内容,
};
#行动就是“做什么”
my$action=param('action')||'display';
$dispatch->{$action}->()||退出|u错误(“错误操作”);
出口
#加载XML并构建一个表来显示它
子显示配置
{my$conf=XML::LibXML->load_XML(location=>$conf);
我的$content;
foreach my$选项($conf->findnodes('/conf/option'))
{$content.=Tr(td($option->findvalue('./name')),
td($option->findvalue('./value'))
);
}
$content=表格($content);
#添加到编辑表单的链接
$content.=p(a({href=>url()。?action=edit'},'edit');
输出($内容);
}
#加载XML并构建一个表单,让您可以编辑它
子显示\编辑\表单
{my$conf=XML::LibXML->load_XML(location=>$conf);
我的$content;
foreach my$选项($conf->findnodes('/conf/option'))
{$content.=Tr(td($option->findvalue('./name')),
td(输入({type=>“text”,大小=>40,
name=>$option->findvalue('name'),
value=>$option->findvalue('./value')}
)
)
);
}
$content=表格($content);
$content=form({action=>url()},
隐藏({name=>action',value=>save',override=>1}),
$content,
提交(‘保存’),
);
输出($内容);
}
#加载XML,检查所有选项,更新表单中的值,
#从表单中保存XML并显示其值
子保存编辑
{my$conf=XML::LibXML->load_XML(location=>$conf);
foreach my$选项($conf->findnodes('/conf/option'))
{my$new_value=param($option->findvalue('./name'));
我的($value_节点)=$option->findnodes('./value');
$value_node->removeChildNodes();
$value\u节点->追加文本($new\u值);
}
$conf->toFile($conf);
显示_conf();
}
#占位符,
子出口错误
{my$message=shift;
打印页眉(),p($message);
}
#输出subs,加载模板,注入内容并输出(带标题)
子输出
{my$content=shift;
打印标题(),在字符串中填充内容(模板(),内容=>$content);
}
在字符串中填充子字符串
{my($template,%param)=@;
$template=~s{}{$param{$1}}g;
返回$template;
}
子模板
{返回联接“”,;}
#模板,非常简单
__资料__
配置编辑器
配置编辑器
配置
部署:将代码放在可以作为CGI运行的地方,并确保web服务器可以读写
conf.xml
。最好将它放在web树之外

这在某种程度上是史前的Perl。CGI被广泛认为是过时的,在Perl中有更现代、更奇特的选项可用。如果您的配置比键/值列表更复杂