perl输出XML与预期不符

时间:2018-12-03 04:43:13

标签: perl

由于某种原因,以下代码生成了我所期望的XML。基本上,下面的4行代码:

Base

产生以下XML:

my $AddStockDate= {
    path => 'dueToStockDate',
    Value => $EXPDATE };

my $AddOrderQty= {
    path => 'releaseQuantity',
    Value => $ORDQTY };

my $AddReleaseId= {
    path => 'userFieldAmount1',
    Value => $RELEASE };

my $AddPartNum= {
    path => 'item',
    Value => $PARTNBR };

$AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Property}[0] = $AddStockDate;
$AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Property}[1] = $AddOrderQty;
$AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Property}[2] = $AddReleaseId;
$AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Property}[3] = $AddPartNum;

实际上,“值”应该是标签,而不是属性,就像Key元素一样。那么为什么将其视为这4行的属性?

在上下文中,这是代码的整个部分:

<Property Value="20181207" path="dueToStockDate"/>
<Property Value="4" path="releaseQuantity"/>
<Property Value="9150021" path="userFieldAmount1"/>
<Property Value="04-13165-02" path="item"/>

现在上述代码中的XML输出:

#!/usr/bin/perl

########################################################################
## Define Perl Packages to Use
########################################################################
use POSIX;
use Data::Dumper;
use LWP::UserAgent;
use HTTP::Request::Common;
use XML::Simple;
use XML::Smart;
use Net::SMTP;

########################################################################
## Define Local Variables
########################################################################
my $lastSeqNum;
my $result;
my ($error, $errStr);
my $storedSeqNum = 0;

my ($var, $val, $configLine);
my ($tmpMon, $tmpYear);
my ($db, $DSN, $sql, $data, $count);

my ($CELLLOC, $CELLCLR,  $SWONBR,  $PONUMBR,  $SWOLINE, $POLINE,   $CELLPID,
    $EXPDATE, $ISSUESRC, $RELEASE, $PARTNAME, $BARCODE, $REVISION, $CELLNAM,
    $TRACKING_NUMBER,
    $PARTNBR, $ORDQTY,   $RELEVT,  $ERRMSG,   $ISSTYPE, $seqNum,     $ERPVNUM);

my ($sigUser, $sigPass, $SQLUser, $SQLPass,
    $smtp, $smtpServer, $mailTo, $mailCC, $mailFrom,
    $confirmIssue, $issueDependent , $SUPPLIER_MATCH);

########################################################################
## Local scripts directory for the Windows machine
########################################################################
$scriptDir = ".";
$agentsDir = "$scriptDir";
$logsDir   = "$scriptDir/logfiles";
#
########################################################################
# Open a new Log file for todays processing
########################################################################
our ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$tmpMon = $mon + 1;
$tmpYear = $year + 1900;
$tmpMon = "0" . $tmpMon if ($tmpMon < 10);
$mday = "0" . $mday if ($mday < 10);
$log_timestamp = localtime(time);
$script_start = time;
my $logFileName = "$0.$tmpYear$tmpMon$mday.log";

########################################################################
## Check to see if log file exists, if already exists concatenate
########################################################################
open(LOGF, ">>$logsDir/$logFileName") || die "Cannot open logfile: $!";
LOGF->autoflush(1);
print LOGF "\n{INFO}  [" . localtime(time) . "] $0 process STARTED\n";

    ## Parse the response for values you will need for subsequent SystemLink calls
    my $SessionHandle = "FakeSession";
    my $workHandle = "WorkHandle";
    my $QueryStatus = "true";
    print LOGF "{DEBUG} [" . localtime(time) . "] \tSession Handle: $SessionHandle\n";
    print LOGF "{DEBUG} [" . localtime(time) . "] \tWork Handle: $workHandle\n";
    print LOGF "{DEBUG} [" . localtime(time) . "] \tQuery Status: $QueryStatus\n";

    ## Now verify whether or not the actual order query was successful or not.  If not, send an error email and move on to next Release
    if($QueryStatus ne 'true') { 
        print LOGF "{ERROR} [" . localtime(time) . "] XML QUERY Post to SystemLink for Release: $RELEASE status is: Query Failure\n";
        print LOGF "{ERROR} [" . localtime(time) . "] XML QUERY Post to SystemLink for Release: $RELEASE COMPLETED with ERRORS. Skipping.\n";
#No SystemLink,Ignore Error#        ErrorHandler("NON-FATAL", "S1002", undef, "SystemLink QUERY XML Post Query Failure", $RELEASE, "$xml_filename");
    }

    my $SLOrderNum = "OrderNum";
    my $SLLineNum = "LineNum";
    print LOGF "{DEBUG} [" . localtime(time) . "] \tOrder Number: $SLOrderNum\n";
    print LOGF "{DEBUG} [" . localtime(time) . "] \tLine Number: $SLLineNum\n";

    if($SLOrderNum eq '') { 
        print LOGF "{ERROR} [" . localtime(time) . "] XML QUERY Post to SystemLink for Release: $RELEASE status is: No Open Order Found\n";
        print LOGF "{ERROR} [" . localtime(time) . "] XML QUERY Post to SystemLink for Release: $RELEASE COMPLETED with ERRORS. Skipping.\n";
#No SystemLink,Ignore Error#        ErrorHandler("NON-FATAL", "S1003", undef, "SystemLink QUERY XML Post No Open Order Found", $RELEASE, "$xml_filename"); 
    }

    ## Now that we've verified all the Order and Line information, we need to create the actual MAPICS order
    print LOGF "{DEBUG} [" . localtime(time) . "] \tBuilding CREATE XML Request STARTED\n";
    my $AddXML = XML::Smart->new();
    my $AddRequest = {
        sessionHandle => $SessionHandle,
        workHandle => $workHandle,
        broker => 'EJB',
        maxIdle => '900000'};
    $AddXML->{'System-Link'}{Request} = $AddRequest;

    my $AddCreate = {
        name => 'createObject_PurchaseOrderItemRelease',
        domainClass => 'com.mapics.pm.PoItemRelease'};
    $AddXML->{'System-Link'}{Request}{Create} = $AddCreate;
    $AddXML->{'System-Link'}{Request}{Create}{ApplyTemplate} = '(none)';

    my $AddOrder = {
        path => 'order',
        Value => $SLOrderNum };

    my $AddLine= {
        path => 'line',
        Value => $SLLineNum };


    $EXPDATE = "2018-01-01";
    my $AddStockDate= {
        path => 'dueToStockDate',
        Value => $EXPDATE };

    $ORDQTY="20";
    my $AddOrderQty= {
        path => 'releaseQuantity',
        Value => $ORDQTY };

    $RELEASE="RELEASEID";
    my $AddReleaseId= {
        path => 'userFieldAmount1',
        Value => $RELEASE };

    $PARTNBR="PART";
    my $AddPartNum= {
        path => 'item',
        Value => $PARTNBR };

    $AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Key}{Property}[0] = $AddOrder;
    $AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Key}{Property}[1] = $AddLine;
    $AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Property}[0] = $AddStockDate;
    $AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Property}[1] = $AddOrderQty;
    $AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Property}[2] = $AddReleaseId;
    $AddXML->{'System-Link'}{Request}{Create}{DomainEntity}{Property}[3] = $AddPartNum;         
    $AddXML->{'System-Link'}{Logout}{sessionHandle} = $SessionHandle;

    $AddXML->apply_dtd($dtdContent);

    # Turn off output of DTD and meta tag generation
    my $xmldata = $AddXML->data(nometagen => 1, nodtd => 1);
    $xmldata =~ s/\?>/?>\n<!DOCTYPE System-Link SYSTEM 'SystemLinkRequest.dtd'>/m;

    print LOGF "{DEBUG} [" . localtime(time) . "] \tBuilding CREATE XML Request COMPLETED\n";
    print LOGF "{DEBUG} [" . localtime(time) . "] \tWriting CREATE XML Request to Request_$xml_filename STARTED\n";

    my $xml_filename = $RELEASE . "-" . time . ".xml";
    open $RequestLogfile, '>>', "$logsDir/XML/Release/Request_$xml_filename";
    print $RequestLogfile "\n--------------- CREATE REQUEST ---------------\n";
    print $RequestLogfile $xmldata;
    close $RequestLogfile;

0 个答案:

没有答案