in reply to copy XML elements from one file to another

As anonymonk points out there is no need to create the parsed elements from scratch:
# parsing File 1 my @nodes_from_file1 = $parser->findnodes('//Feature'); # or whatever it is you need in each node # later my $new = XML::LibXML::Document->new('1.0', 'utf-8'); my $features = $new->createElement( 'Features' ); my $file1 = $new->createElement( 'File1' ); for my $node (@nodes_from_file1) { $file1->addChild( $node ); } $features->addChild( $file1 ); # and so on
Using your sample data (which has errors btw) this would produce:
<Features> <File1> <Feature> <Number>86839</Number> <Prefix>419</Prefix> <Alt>73924</Alt> </Feature> <Feature> <Number>82783</Number> <Prefix>826</Prefix> <Alt>27800</Alt> </Feature> <Feature> <Number>82783</Number> <Prefix>827</Prefix> <Alt>26433</Alt> </Feature> </File1> </Features>

Replies are listed 'Best First'.
Re^2: copy XML elements from one file to another
by Anonymous Monk on Nov 25, 2013 at 15:29 UTC

    Thanks for your help. I was trying your soltuion but I cant figure out the follwoing:
    1. how I can view my output file? I don't see any generated output files
    2. I assume "parser" in this case will be parsing the source file (where im getting my input from) right ?

    I understand whats going on with your code but im just lost with where I see my results. I have tried multiple times but couldn't see any generated output file.
    tried different examples using the same approach but still cant figure out how to get my output file.

      how I can view my output file? I don't see any generated output files
      You can print directly to a file using the toFile() method. Add the following lines to the example given above:
      $new->addChild($features); my $filename = 'new.xml'; my $ok = $new->toFile($filename,2);
      I assume "parser" in this case will be parsing the source file (where im getting my input from) right?
      Yes, you would set up the parser like this, probably in a loop to go through each file:
      my $file = 'file-1.xml'; my $parser = XML::LibXML->load_xml(location => $file);

        Thanks again for the help but unfortunately I only got the following in my output file when using $new->toFile('fileName')
        <?xml version="1.0" encoding="utf-8"?>
        I have tried this before reading your reply. But I got the same result. printing my results did help a bit just for debugging purposes. Also tried piping my print statements to a file but the formatting was not good and the text was from bottom to top
        I just need to get this step done by seeing a nice formatted output so I can start playing around with the code and get what I want. Thanks again

        I did actually include that line. You mean the same line you mentioned in your code in your first response right ?
        I used your code exactly with tweaking few things like file names. The print is showing that its working overall but file output is unfortunately not
        btw when I used your suggestion my $ok = $new->toFile($file-name,2) I got I/O invalid argument. I managed to get it work but as I said still doesn't show anything other than what I mentioned.
        again thanks for your help

Re^2: copy XML elements from one file to another
by Anonymous Monk on Nov 26, 2013 at 16:53 UTC

    Here is my code:

    use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("FILE1.xml"); my @nodes_from_file1 = $doc->findnodes('//Feature'); my $new = XML::LibXML::Document->new('1.0', 'utf-8'); my $features = $new->createElement( 'Features' ); #print to debug print $features->toString; my $file1 = $new->createElement( 'File1' ); for my $node (@nodes_from_file1) { $file1->addChild( $node ); #print to debug print $file1->toString; } $features->addChild( $file1 ); print $new->toString; $new->toFile("output_new.xml")
      I can't see the line $new->addChild($features);

      You need to include the format argument 2 in $new->toFile("output_new.xml",2) and print $new->toString(2);

        Thanks for your effort everything seem to be ok now. Just one last thing. The following is my current output.

        <?xml version="1.0" encoding="utf-8"?> <LINE Value="some_text"> <status/> <FEATURES/> <File1_data> <ABC id="1"> <Feature> <Number>im from file 1</Number> <Prefix>419</Prefix> <Alt>73924/</Alt> </Feature> </ABC> <ABC id="2"> <Feature> <Number>im from file 1</Number> <Prefix>826</Prefix> <Alt>27800</Alt> </Feature> </ABC> <ABC id="3"> <Feature> <Number>im from file 1</Number> <Prefix>827</Prefix> <Alt>26433</Alt> </Feature> </ABC> </File1_data> </LINE>

        as you can see that </ABC> (copied from Files_1 as a node) is not aligned properly so I just would like to learn how I can create nested elements and close them properly.
        I have seen lots of examples but nothing is nested in the way I need it. I have tried many times but not sure whats going on. Following is what I need to get.

        <?xml version="1.0" encoding="utf-8"?> <LINE Value="some text"> <status/> <FEATURES> <File1_data> <ABC id="1"> <Feature> <Number>im from file 1</Number> <Prefix>419</Prefix> <Alt>73924/</Alt> </Feature> </ABC> <ABC id="2"> <Feature> <Number>im from file 1</Number> <Prefix>826</Prefix> <Alt>27800</Alt> </Feature> </ABC> <ABC id="3"> <Feature> <Number>im from file 1</Number> <Prefix>827</Prefix> <Alt>26433</Alt> </Feature> </ABC> </File1_data> </FEATURES> </LINE>

        sorry for asking to much but any idea if I can access the elements Im copying and modify its name?
        for example lets say I would like to add numbers to the Features elements im copying i.e <Feature 1> <Feature 2>....etc.