combining xml's

Discussion in 'Perl Misc' started by jack, Feb 20, 2006.

  1. jack

    jack Guest

    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
     
    jack, Feb 20, 2006
    #1
    1. Advertising

  2. "jack" <> wrote in news:1140465119.149434.63680
    @z14g2000cwz.googlegroups.com:

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 20, 2006
    #2
    1. Advertising

  3. jack wrote:
    > 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
     
    Jürgen Exner, Feb 20, 2006
    #3
  4. jack

    Matt Garrish Guest

    "A. Sinan Unur" <> wrote in message
    news:Xns977099F1A2F4asu1cornelledu@127.0.0.1...
    > "jack" <> wrote in news:1140465119.149434.63680
    > @z14g2000cwz.googlegroups.com:
    >
    >> 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.
    >


    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
     
    Matt Garrish, Feb 20, 2006
    #4
  5. jack

    Matt Garrish Guest

    "Matt Garrish" <> wrote in message
    news:iQrKf.946$...
    >
    > "A. Sinan Unur" <> wrote in message
    > news:Xns977099F1A2F4asu1cornelledu@127.0.0.1...
    >> "jack" <> wrote in news:1140465119.149434.63680
    >> @z14g2000cwz.googlegroups.com:
    >>
    >>> 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.
    >>

    >
    > 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
     
    Matt Garrish, Feb 20, 2006
    #5
  6. jack

    thrill5 Guest

    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

    "jack" <> wrote in message
    news:...
    > 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
    >
     
    thrill5, Feb 21, 2006
    #6
  7. jack

    Tintin Guest

    "jack" <> wrote in message
    news:...
    > 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.
     
    Tintin, Feb 21, 2006
    #7
  8. jack <> wrote:

    > 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";
    ----------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 21, 2006
    #8
  9. jack

    Guest

    jack wrote:
    >
    > 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
     
    , Feb 21, 2006
    #9
  10. jack

    robic0 Guest

    On Mon, 20 Feb 2006 18:02:59 -0500, "Matt Garrish" <> wrote:

    >
    >"Matt Garrish" <> wrote in message
    >news:iQrKf.946$...
    >>
    >> "A. Sinan Unur" <> wrote in message
    >> news:Xns977099F1A2F4asu1cornelledu@127.0.0.1...
    >>> "jack" <> wrote in news:1140465119.149434.63680
    >>> @z14g2000cwz.googlegroups.com:
    >>>
    >>>> 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.
    >>>

    >>
    >> 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
    >

    Sheet, yeah right, your probably ising it right now.......
     
    robic0, Feb 21, 2006
    #10
  11. jack

    robic0 Guest

    On Tue, 21 Feb 2006 15:55:58 -0800, robic0 wrote:

    >On Mon, 20 Feb 2006 18:02:59 -0500, "Matt Garrish" <> wrote:
    >
    >>
    >>"Matt Garrish" <> wrote in message
    >>news:iQrKf.946$...
    >>>
    >>> "A. Sinan Unur" <> wrote in message
    >>> news:Xns977099F1A2F4asu1cornelledu@127.0.0.1...
    >>>> "jack" <> wrote in news:1140465119.149434.63680
    >>>> @z14g2000cwz.googlegroups.com:
    >>>>
    >>>>> 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.
    >>>>
    >>>
    >>> 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
    >>

    >Sheet, yeah right, your probably ising it right now.......

    And I havent even posted the 2,000 line code yet
     
    robic0, Feb 21, 2006
    #11
  12. jack

    jack Guest

    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
     
    jack, Feb 23, 2006
    #12
  13. jack

    jack Guest

    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
     
    jack, Feb 23, 2006
    #13
  14. jack

    J. Gleixner Guest

    jack wrote:
    > 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 $@;
     
    J. Gleixner, Feb 23, 2006
    #14
  15. jack

    J. Gleixner Guest

    J. Gleixner wrote:
    > jack wrote:
    >> 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?..

    ^^ within
     
    J. Gleixner, Feb 23, 2006
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    522
  2. BaKMaN
    Replies:
    0
    Views:
    386
    BaKMaN
    Jan 30, 2004
  3. johdah
    Replies:
    0
    Views:
    365
    johdah
    Mar 4, 2004
  4. barmalini

    Combining XHTML SVG and XML

    barmalini, Aug 20, 2004, in forum: XML
    Replies:
    2
    Views:
    630
    William Park
    Aug 21, 2004
  5. lievemario

    removing/combining doubles in xml

    lievemario, Nov 29, 2004, in forum: XML
    Replies:
    1
    Views:
    430
    Joris Gillis
    Nov 29, 2004
Loading...

Share This Page