combining xml's

J

jack

Hi,

I am working on a perl script which actually combines multiple XMLs
into a single XML. The scenario looks like this:

one.xml

<people>
<student>John</student>
<student>Jim</student>
</people>

two.xml

<people>
<student>steve</student>
<student>will</student>
</people>

result.xml #This is the file I need to create by combining the
above two files

<people>
<student>John</student>
<student>Jim</student>
<student>steve</student>
<student>will</student>
</people>

Assume there are multiple tags similar to this. How do it do it?


-Thanks in advance,
Jack
 
A

A. Sinan Unur

I am working on a perl script which actually combines multiple XMLs
into a single XML. The scenario looks like this:

one.xml

<people>
<student>John</student>
<student>Jim</student>
</people>

two.xml

<people>
<student>steve</student>
<student>will</student>
</people>

result.xml #This is the file I need to create by combining the
above two files

<people>
<student>John</student>
<student>Jim</student>
<student>steve</student>
<student>will</student>
</people>

Assume there are multiple tags similar to this. How do it do it?

Use a hash.

If you want more detail than that, you might want to consider some of
your own time in formulation a question.

The posting guidelines might be of some help.

Sinan
 
J

Jürgen Exner

jack said:
I am working on a perl script which actually combines multiple XMLs
into a single XML. The scenario looks like this:

one.xml

<people>
<student>John</student>
<student>Jim</student>
</people>

two.xml

<people>
<student>steve</student>
<student>will</student>
</people>

result.xml #This is the file I need to create by combining the
above two files

<people>
<student>John</student>
<student>Jim</student>
<student>steve</student>
<student>will</student>
</people>

Assume there are multiple tags similar to this. How do it do it?

I would use one of the several XML parser from CPAN.
And then simply walk through the parsed trees and print the output XML.

jue
 
M

Matt Garrish

A. Sinan Unur said:
Use a hash.

If you want more detail than that, you might want to consider some of
your own time in formulation a question.

Hashes are only useful for really Simple XML (hint to OP). If order matters,
which it often does, I would go to XML-LibXML create a couple of trees and
walk one to add to the other.

Matt
 
M

Matt Garrish

Matt Garrish said:
Hashes are only useful for really Simple XML (hint to OP). If order
matters, which it often does, I would go to XML-LibXML create a couple of
trees and walk one to add to the other.

Sorry, first I'd waste all my time on robic0's useless parser and *then* I'd
do it the right way. Again, sorry if I caused any confusion... ; )

Matt
 
T

thrill5

While Perl might be the answer, another is to use XSLT to combine the
documents. You still need a script language to invoke XSL translation but
just Google "XSL combine XML documents" and you will find quite a few
solutions.

Scott
 
T

Tintin

jack said:
Hi,

I am working on a perl script which actually combines multiple XMLs
into a single XML. The scenario looks like this:

one.xml

<people>
<student>John</student>
<student>Jim</student>
</people>

two.xml

<people>
<student>steve</student>
<student>will</student>
</people>

result.xml #This is the file I need to create by combining the
above two files

<people>
<student>John</student>
<student>Jim</student>
<student>steve</student>
<student>will</student>
</people>

Assume there are multiple tags similar to this. How do it do it?

If you had valid XML, you could use an XML parser, but given that you have
only XML like data, then it will be more difficult.
 
T

Tad McClellan

jack said:
one.xml

<people>
<student>John</student>
<student>Jim</student>
</people>

two.xml

<people>
<student>steve</student>
<student>will</student>
</people>

result.xml #This is the file I need to create by combining the
above two files

<people>
<student>John</student>
<student>Jim</student>
<student>steve</student>
<student>will</student>
</people>

Assume there are multiple tags similar to this. How do it do it?


----------------------
#!/usr/bin/perl
use warnings;
use strict;

print "<people>\n";
local @ARGV = qw/ one.xml two.xml /;
while ( <> ) {
print if /<student>/;
}
print "</people>\n";
 
J

jl_post

jack said:
I am working on a perl script which actually combines multiple XMLs
into a single XML. The scenario looks like this:

one.xml

<people>
<student>John</student>
<student>Jim</student>
</people>

two.xml

<people>
<student>steve</student>
<student>will</student>
</people>

result.xml #This is the file I need to create by combining the
above two files

<people>
<student>John</student>
<student>Jim</student>
<student>steve</student>
<student>will</student>
</people>


Dear Jack,

If your XML files are simple XML files, then you can use the
XML::Simple module to help you out. Just type "perldoc XML::Simple" to
read more about it if you're interested.

Anyway, here's a short script that does exactly what you want (it
reads "one.xml" and "two.xml" and writes out "result.xml"):


#!/usr/bin/perl
use strict;
use warnings;

use XML::Simple;

# Read in input files:
my $one = XMLin("one.xml", forcearray => 1,
keeproot => 1,
keyattr => []);
my $two = XMLin("two.xml", forcearray => 1,
keeproot => 1,
keyattr => []);

# Populate the $result object:
my $result = $one;
push( @{$result->{people}[0]{student}},
@{$two->{people}[0]{student}} );

# Write out the output file:
XMLout($result, keeproot => 1,
keyattr => [],
outputfile => "result.xml");

__END__


I hope this helps, Jack.

-- Jean-Luc
 
R

robic0

Sorry, first I'd waste all my time on robic0's useless parser and *then* I'd
do it the right way. Again, sorry if I caused any confusion... ; )

Matt
Sheet, yeah right, your probably ising it right now.......
 
J

jack

Hi guys..

Thanks a lot for your advice..I am now able to combine xml files but
when I am trying to print the HTML in the perl file...it doesn't show
up the HTML. A blank page is shown. When I checked the Apache error
logs...it says permission denied. Its stopping at XMLout where I write
the combined data into an xml file.

i.e., something similar to

!/usr/bin/perl -w

get form parameters

sub combine
{

$one = xmlin("one.xml");
$two = XMLin("two.xml");
$result = $two; #Initialise $result as $two
#combine both $one and $two and write the result into $result.
#now write the $result into result.xml

XMLout($result, keeproot => 1,
keyattr => [],
outputfile => "result.xml")
}

print "Content-type: text/html\n\n";
print <<EOF;
<html><head></head>
EOF
combine();
print <<EOF1;
<body>
<table><tr><td>Hello world</td></tr></table>
</body>
</html>
EOF1


When I comment out the XMLout part, it prints the "Hello world" . But
when I uncomment it, nothing shows up on the screen and the error logs
say that permission denied to write to result.xml. How do I proceed?

Thanks,
Jack
 
J

jack

Hi guys..

Thanks a lot for your advice..I am now able to combine xml files but
when I am trying to print the HTML in the perl file...it doesn't show
up the HTML. A blank page is shown. When I checked the Apache error
logs...it says permission denied. Its stopping at XMLout where I write
the combined data into an xml file.

i.e., something similar to

!/usr/bin/perl -w

get form parameters

sub combine
{

$one = xmlin("one.xml");
$two = XMLin("two.xml");
$result = $two; #Initialise $result as $two
#combine both $one and $two and write the result into $result.
#now write the $result into result.xml

XMLout($result, keeproot => 1,
keyattr => [],
outputfile => "result.xml")
}

print "Content-type: text/html\n\n";
print <<EOF;
<html><head></head>
EOF
combine();
print <<EOF1;
<body>
<table><tr><td>Hello world</td></tr></table>
</body>
</html>
EOF1


When I comment out the XMLout part, it prints the "Hello world" . But
when I uncomment it, nothing shows up on the screen and the error logs
say that permission denied to write to result.xml. How do I proceed?

Thanks,
Jack
 
J

J. Gleixner

jack said:
Hi guys..

Thanks a lot for your advice..I am now able to combine xml files but
when I am trying to print the HTML in the perl file...it doesn't show

How do you "print the HTML in the perl file"? Are you trying to print
the XML to your HTML?..
up the HTML. A blank page is shown. When I checked the Apache error
logs...it says permission denied. Its stopping at XMLout where I write
the combined data into an xml file.

What part of "permission denied" do you not understand?

You are asking XMLout to create a file 'result.xml', which isn't
writable by the id running your Web server.

i.e., something similar to

!/usr/bin/perl -w

get form parameters

Take a look at the CGI module.
sub combine
{

$one = xmlin("one.xml");
$two = XMLin("two.xml");

Why different case? Also, you'd probably want some type of error
checking, to ensure XMLin was successful.
$result = $two; #Initialise $result as $two
#combine both $one and $two and write the result into $result.
#now write the $result into result.xml

XMLout($result, keeproot => 1,
keyattr => [],
outputfile => "result.xml")

Look at the documentation for XMLout. By default, it'll send its output
to STDOUT.
}

print "Content-type: text/html\n\n";
print <<EOF;
<html><head></head>
EOF
combine();

Why are you calling it at this point?
print <<EOF1;
<body>
<table><tr><td>Hello world</td></tr></table>
</body>
</html>
EOF1


When I comment out the XMLout part, it prints the "Hello world" . But
when I uncomment it, nothing shows up on the screen and the error logs
say that permission denied to write to result.xml. How do I proceed?

What *should* show up in your HTML page? combine() has a fatal error,
how would you like it to proceed?

Ignore it?

eval { combine() };

Handle it:

eval { combine() };
print $@;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top