Note the input data you've shown isn't valid XML, and you haven't shown the expected output, so I have to guess at what you want. See How do I post a question effectively? and Short, Self-Contained, Correct Example.

As documented, ->findnodes in scalar context returns an XML::LibXML::NodeList object, which you can't pass to ->createElement. Here's one approach:

use warnings; use strict; use XML::LibXML; my @files = ("file1.xml", "file2.xml", "file3.xml"); my $outfile = "result.xml"; my $newdom = XML::LibXML::Document->new('1.0', 'UTF-8'); my $root = $newdom->createElement('testsuites'); $newdom->setDocumentElement($root); for my $file (@files) { my $dom = XML::LibXML->load_xml(location => $file); for my $node ( $dom->findnodes('//testsuite') ) { $node->{name} =~ s/test_suite_name_/tsname_/; $root->appendChild($node); } } $newdom->toFile($outfile, 1);

With input files that look like this:

<?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="test_suite_name_one"> <testcase name="test1" /> <testcase name="test2" /> </testsuite> </testsuites>

It produces output like this:

<?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="tsname_one"> <testcase name="test1"/> <testcase name="test2"/> </testsuite> <testsuite name="tsname_two"> <testcase name="test3"/> <testcase name="test4"/> </testsuite> <testsuite name="tsname_three"> <testcase name="test5"/> <testcase name="test6"/> </testsuite> </testsuites>

In reply to Re: Parsing XML data and injecting it into new file by haukex
in thread Parsing XML data and injecting it into new file by hosselausso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.