http://qs1969.pair.com?node_id=11136470


in reply to Parsing XML data and injecting it into new file

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>

Replies are listed 'Best First'.
Re^2: Parsing XML data and injecting it into new file
by hosselausso (Initiate) on Sep 05, 2021 at 16:07 UTC
    This is exactly what I was asking. Thank you very much and sorry for missing the expected outcome.