CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How can i use xml-moduls for my problem?

 



0815
Novice

Apr 16, 2009, 3:19 AM

Post #1 of 10 (3834 views)
How can i use xml-moduls for my problem? Can't Post

Hello,

how can i use xml-moduls for my problem? I have a xml file with lot of variables, which a like to replace. My variable (like XML_SID) are only in CDATA.



This are my first test to read it:


Code
#!/usr/bin/perl 
use XML::Simple;
use Data::Dumper;

$xml = new XML::Simple;

@xmlkeys=();
@xmlvals=();

$data = $xml->XMLin('inifile.xml', KeepRoot => 1);
&listhash ( $data, 0,"" );
XMLout($data, KeepRoot => 1, OutputFile => 'xinifile.xml');
print Dumper($data);

print "#############################################\n";

$numkeys=$#xmlkeys;

print $#xmlkeys,"\n";
print $#xmlvals,"\n";



foreach $t (@xmlvals) {
print $t,"\n";
}

print "#############################################\n";

foreach $t (@xmlkeys) {
print $t,"\n";
}

print "HIER\n";

###############################################################################
sub listhash {
my %myhash = %{$_[0]};
my $mylevel = $_[1];
my $parentkey=$_[2];
my $mykey = '';
my $myval = '';
my $myderefval = '';

foreach $mykey( keys(%myhash) ) {
# print "$mylevel KEY $mykey\n";
# print "$parentkey KEY $mykey\n";
$myval= $myhash{$mykey};
$myderefval=\$myhash{$mykey};

if ($myderefval =~ "^SCALAR") {
push (@xmlkeys, $parentkey.'**'.$mykey);
$myval =~ s/\s+$//g;
push (@xmlvals, $myval );

#if ($myval =~ "^SAPXML_DBSCHEMA" ) {
# print "HIER\n";
# die;
# $myhash{$mykey} = 'JOW_DBSCHEMA';
# }
}

if ($myderefval =~ "^REF") {
if ($myval =~ "^HASH") {
&listhash ( $myval, $mylevel+1, $parentkey.'**'.$mykey );
}
}
}
# return $mystring;

}


TX for any help!


(This post was edited by 0815 on Apr 19, 2009, 7:14 AM)


1arryb
User

Apr 16, 2009, 7:43 AM

Post #2 of 10 (3830 views)
Re: [0815] How can i use xml-moduls for my problem? [In reply to] Can't Post

Hi 0815,

The link you provided, http://paste.pocoo.org/show/112749/, points to an html document that describes an xml file, not the xml file itself (if you don't believe me, go to the link and "View-->Page Source" in your browser). If you're really trying to parse this as xml, that would be a problem.

Also, in future posts, please enclose code samples using the "code" tag. It makes them alot easier to read.

Cheers,

Larry


0815
Novice

Apr 16, 2009, 7:58 AM

Post #3 of 10 (3824 views)
Re: [1arryb] How can i use xml-moduls for my problem? [In reply to] Can't Post

Hi Larry,

i have changed it.


Jörg


1arryb
User

Apr 16, 2009, 8:37 AM

Post #4 of 10 (3820 views)
Re: [0815] How can i use xml-moduls for my problem? [In reply to] Can't Post

Hi 0815,

Ok, your program is a mess, but I think your real question is how to modify values and output back to xml while preserving the CDATA tags? If so, perhaps XML::Twig is what you want. It is discussed in this thread over at perl monks http://www.perlmonks.org/?node_id=701522.

Good luck,

Larry


0815
Novice

Apr 16, 2009, 11:41 AM

Post #5 of 10 (3812 views)
Re: [1arryb] How can i use xml-moduls for my problem? [In reply to] Can't Post

Hi Larry,

thanks. Yes, this is the real question. Can you help me please? I have read the documentation of twig, but i don't unterstand this. This is the first time,
with modules for me.


1arryb
User

Apr 16, 2009, 3:30 PM

Post #6 of 10 (3807 views)
Re: [0815] How can i use xml-moduls for my problem? [In reply to] Can't Post

Hi 0815,

Of course, you could just use a regular expression filter on the file:

Code
perl -pi -e  's/XML_SID/My Concretized XML_SID Value/' inifile.xml


But if you really need to go the xml parse route, here's a little XML::Twig program that might give you a starting point:

Code
#!/usr/bin/perl 

use strict;
use warnings;

use XML::Twig;

sub handle_cdata {
my ($twig, $ele) = @_;

# I had trouble parsing just the #CDATA tags, so I start
# a couple of levels up.
my $strval = $ele->first_child('strval');
return unless $strval;
my $cdata = $strval->first_child('#CDATA');
return unless $cdata;

if ($cdata->text eq "XML_SID") {
# Replace XML_SID meta with something else.
my $ncdata = new XML::Twig::Elt( '#CDATA', "My Concretized XML_SID value");
$ncdata->replace($cdata);
}
}

# If other tags besides 'fld's contain stuff you want to
# transform, put them here.
my $handlers = {
'fld' => \&handle_cdata
};

my $twig = new XML::Twig(twig_handlers => $handlers);
$twig->parsefile("inifile.xml");

# Change to $twig->flush if you want edit-in-place semantics. Othersise, redirect to a file from the command line.
$twig->set_pretty_print("indented");
$twig->print;


UPDATE: BTW, the XML::Twig perldoc references an excellent tutorial on the web at http://www.xmltwig.com/tutorial/. Check it out.

Cheers,

Larry


(This post was edited by 1arryb on Apr 16, 2009, 3:46 PM)


0815
Novice

Apr 17, 2009, 11:32 AM

Post #7 of 10 (3786 views)
Re: [1arryb] How can i use xml-moduls for my problem? [In reply to] Can't Post

Wow!

Thanks for your support Larry.


Have a nice weekend
Jörg


0815
Novice

Jun 2, 2009, 8:18 AM

Post #8 of 10 (3720 views)
Re: [0815] How can i use xml-moduls for my problem? [In reply to] Can't Post

Hello Larry,

your sample for me start with 'fld', call the handler and parse.
What is, if i will change CDATA field in fld name="migmonJobNum"? Or if i will take <property name ="doc-long" value ="
Number of parallel Migration Monitor jobs. " /> </properties> to identify of CDATA

Now, i will create the xml file with xml variables. To begin are CDATA = 3 (later CDATA = XML_NO_OF_PARALLEL_R3LOAD). Too little for parsing..


Code
      <fld name="migmonJobNum"> 
<properties>
<property name ="doc-long" value ="
Number of parallel Migration Monitor jobs. " />
</properties>
<strval><![CDATA[XML_NO_OF_PARALLEL_R3LOAD]]>
</strval>


Thanks
Jörg


(This post was edited by 0815 on Jun 2, 2009, 9:03 AM)


1arryb
User

Jun 3, 2009, 11:14 AM

Post #9 of 10 (3699 views)
Re: [0815] How can i use xml-moduls for my problem? [In reply to] Can't Post

hi Jorg,

What have you tried so far?

Cheers,

Larry


0815
Novice

Jun 3, 2009, 11:42 AM

Post #10 of 10 (3696 views)
Re: [1arryb] How can i use xml-moduls for my problem? [In reply to] Can't Post

 

product id:


Code
<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!DOCTYPE tables SYSTEM "keydb.dtd" >
<!-- -->
<tables>
<tableset srcid="generated">
<table name="SAPINST_EXE" namespaces="STANDARD">
<columns>
<column name="COMPONENT">
<defaultproperties>
<property name ="ANCESTORS_DISPLAY_NAME" value ="SAP SCM 5.0 Support Release 3 &gt; SAP Systems &gt; Oracle &gt; Distributed System &gt; Based on AS ABAP &gt; " />
<property name ="DISPLAY_NAME" value ="Global Host Preparation" />
<property name ="INSTALLATION_START" value ="20080814125437" />
<property name ="INSTALLATION_STOP" value ="20080814125553" />
<property name ="PPMS_COMPONENT_NAME" value ="ind" />
<property name ="PPMS_COMPONENT_RELEASE" value ="ind" />
<property name ="PRODUCT_ID_FROM_CATALOG_FILE" value ="d0e23657" />
</defaultproperties>


I parse with regex the file it is the better solution. Because
my string are in the first few lines.




Code
# search product id in keydb.xml and instert as comment in inifile.xml 

sub product_id {
my $keydb = $_[0];
my $product_id = "";

open( FILE, '<', $keydb ) || die "Fehler beim Oeffnen zum Lesen: $!";
while (<FILE>) {
chomp;
if ( $_ =~ /PRODUCT_ID_FROM_CATALOG_FILE/ ) {
$_ =~ s/.*value ="//g;
$_ =~ s/" \/>//g;
$product_id = $_;
print "produkt id :" . $product_id . "\n";
print "inifile_out :" . $inifile_out . "\n";
last;
}
}
close FILE;

open( INIFILE, ">>$inifile_out" ) || die "Can not write $inifile_out";
print INIFILE "<!-- #" . $product_id . "# -->";
close INIFILE;
return $product_id;

}



(This post was edited by 0815 on Jun 22, 2009, 11:55 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives