Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
使用XML::Compile从XSD生成XML的示例Perl代码_Perl_Xsd - Fatal编程技术网

使用XML::Compile从XSD生成XML的示例Perl代码

使用XML::Compile从XSD生成XML的示例Perl代码,perl,xsd,Perl,Xsd,有谁能给我举一个使用XSD生成XML的例子吗 我试图发布我的脚本,我正在尝试与XSD一起发布,但我无法做到这一点。因此,我正在寻找一个任何示例 我不久前写了一篇关于这个的教程:我不久前写了一篇关于这个的教程:简而言之,你需要做: 将XSD格式转换为Perl哈希结构 构造这个散列,填充数据 将哈希转换为XML 所需套餐: XML::Compile::Schema XML::LibXML::Document 下面的代码从XSD定义创建一个Perl结构 use XML::Compile::Sche

有谁能给我举一个使用XSD生成XML的例子吗


我试图发布我的脚本,我正在尝试与XSD一起发布,但我无法做到这一点。因此,我正在寻找一个任何示例

我不久前写了一篇关于这个的教程:

我不久前写了一篇关于这个的教程:

简而言之,你需要做:

  • 将XSD格式转换为Perl哈希结构
  • 构造这个散列,填充数据
  • 将哈希转换为XML
  • 所需套餐:

    • XML::Compile::Schema
    • XML::LibXML::Document
    下面的代码从XSD定义创建一个Perl结构

    use XML::Compile::Schema;
    use Data::Dumper;
    
    my $filename = $ARGV[0] || "";
    
    if(!$filename){
    warn "Please provide the WSDL definition file.";
    exit 10;
    }
    
    my $schema = XML::Compile::Schema->new($filename);
    my $hash;
    
    print Dumper $schema->template('PERL' => 'Application');
    
    然后,此程序创建的Perl数据结构如下所示:

    {
      MakeName =>
        {
         UniqueID => "anything",
         _ => "example", },
    
           MakeDetails =>
         {  
          Name =>
          {
           UniqueID => "anything",
           _ => "example", }, 
         },
       };
    
    因此,您的其他工作将在程序中创建相同的结构,填写如下内容:

      my $hash = {
        MakeName => {
          UniqueID => 'xxxx',
          _ => 'Name of the Make',
        },
    
        OtherFields => foo_bar_get_other_hash(),
       };
     ....
    
     ## breathtaking moment, create the XML from this $hash 
    
     my $schema = XML::Compile::Schema->new("/opt/data/your.xsd");
    
     my $doc = XML::LibXML::Document->new();
     my $writer = $schema->compile(WRITER => 'Application');
    
     my $xml;
    
     ## Create $xml in the memory based on the Schema and your $hash
     eval{ $xml = $writer->($doc, $hash);};   
    
     if($@){
    # Useful if the format is invalid against the Schema definition
        # Or if there are other errors may occurs
        $err_msg = $@->{message}->toString();
    return ("", $err_msg);
     }
    
     ## If you want save this $xml to file, convert it to string format first
     $doc->setDocumentElement($xml);
     my $ori_content = $doc->toString(1);
     ## Now $ori_content holds the full XML content.
    

    简而言之,您需要执行以下操作:

  • 将XSD格式转换为Perl哈希结构
  • 构造这个散列,填充数据
  • 将哈希转换为XML
  • 所需套餐:

    • XML::Compile::Schema
    • XML::LibXML::Document
    下面的代码从XSD定义创建一个Perl结构

    use XML::Compile::Schema;
    use Data::Dumper;
    
    my $filename = $ARGV[0] || "";
    
    if(!$filename){
    warn "Please provide the WSDL definition file.";
    exit 10;
    }
    
    my $schema = XML::Compile::Schema->new($filename);
    my $hash;
    
    print Dumper $schema->template('PERL' => 'Application');
    
    然后,此程序创建的Perl数据结构如下所示:

    {
      MakeName =>
        {
         UniqueID => "anything",
         _ => "example", },
    
           MakeDetails =>
         {  
          Name =>
          {
           UniqueID => "anything",
           _ => "example", }, 
         },
       };
    
    因此,您的其他工作将在程序中创建相同的结构,填写如下内容:

      my $hash = {
        MakeName => {
          UniqueID => 'xxxx',
          _ => 'Name of the Make',
        },
    
        OtherFields => foo_bar_get_other_hash(),
       };
     ....
    
     ## breathtaking moment, create the XML from this $hash 
    
     my $schema = XML::Compile::Schema->new("/opt/data/your.xsd");
    
     my $doc = XML::LibXML::Document->new();
     my $writer = $schema->compile(WRITER => 'Application');
    
     my $xml;
    
     ## Create $xml in the memory based on the Schema and your $hash
     eval{ $xml = $writer->($doc, $hash);};   
    
     if($@){
    # Useful if the format is invalid against the Schema definition
        # Or if there are other errors may occurs
        $err_msg = $@->{message}->toString();
    return ("", $err_msg);
     }
    
     ## If you want save this $xml to file, convert it to string format first
     $doc->setDocumentElement($xml);
     my $ori_content = $doc->toString(1);
     ## Now $ori_content holds the full XML content.