Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Parsing XML data and injecting it into new file

by haukex (Archbishop)
on Sep 05, 2021 at 15:59 UTC ( [id://11136470]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11136470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-23 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found