Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I assume this is a followup to your previous post "Run a perl code on all input files and have the results in different output files", so what you've shown here is the Perl data structure returned by TAP3::Tap3edit's $tap3->structure run through XML::Dumper. That module allows me to parse back the XML into the data structure I show below (anonymized, in case this happens to be real data? Update: SaraMirabi confirmed in the CB that it is fake data) - it would have been better if you'd showed us the Perl data structure to begin with, using Data::Dumper or Data::Dump.

I think it would be best if you didn't rely on munging the output of XML::Dumper, but instead build the XML output from the Perl data structure yourself, because that gives you full control over the produced XML. Personally, although it's a bit more verbose, I prefer to write XML with XML::LibXML and some helper functions. The following is an example of how one might go about that, the idea being that you can adapt this however you need.

Update: Significantly simplified toxml and made it more flexible (output is unchanged).

#!/usr/bin/env perl use warnings; use strict; use open qw/:std :utf8/; use XML::LibXML; my $data = { employee => [ { "************" => "M", age => { dob => "01-04-1993" }, department => { departmentname => "Operations", title => "Manager" }, location => { town => { county => "Somewhere", name => "Someplace" } }, name => { forename => "John", surname => "Doe" }, }, { "************" => "M", age => { dob => "12-12-1979" }, department => { departmentname => "Internet", title => "Developer" }, location => { town => { county => "Somewhere", name => "Othertown" } }, name => { forename => "Jane", surname => "Doe" }, } ] }; my $doc = XML::LibXML::Document->createDocument('1.0', 'UTF-8'); toxml($doc, 'data', $data); print $doc->toString(1); sub toxml { my ($parent, $name, $data) = @_; my @args = $name=~/\A\w+\z/ ? ($name) : ('value', name=>$name); if ( ref $data eq 'HASH' ) { my $el = newel($parent, @args); toxml($el, $_, $data->{$_}) for sort keys %$data; } elsif ( ref $data eq 'ARRAY' ) { toxml(ref eq 'ARRAY' ? newel($parent, @args) : $parent, $name, $_) for @$data; } elsif ( ref $data ) { die "Can't handle $data (yet)" } else { newel($parent, @args)->appendText($data) } } sub newel { my ($parent, $name, %attrs) = @_; my $el = $parent->ownerDocument->createElement($name); $el->setAttribute( $_ => $attrs{$_} ) for keys %attrs; if ( $parent->nodeType==XML_DOCUMENT_NODE ) { $parent->setDocumentElement($el) } else { $parent->appendChild($el) } return $el; } __END__ <?xml version="1.0" encoding="UTF-8"?> <data> <employee> <value name="************">M</value> <age> <dob>01-04-1993</dob> </age> <department> <departmentname>Operations</departmentname> <title>Manager</title> </department> <location> <town> <county>Somewhere</county> <name>Someplace</name> </town> </location> <name> <forename>John</forename> <surname>Doe</surname> </name> </employee> <employee> <value name="************">M</value> <age> <dob>12-12-1979</dob> </age> <department> <departmentname>Internet</departmentname> <title>Developer</title> </department> <location> <town> <county>Somewhere</county> <name>Othertown</name> </town> </location> <name> <forename>Jane</forename> <surname>Doe</surname> </name> </employee> </data>

In reply to Re: removing perldata , hashref from XML file (updated) by haukex
in thread removing perldata , hashref from XML file by SaraMirabi

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found