http://qs321.pair.com?node_id=1076242

sriram83.life has asked for the wisdom of the Perl Monks concerning the following question:

Hello Every one,

Here is my input xml:

<?xml version="1.0"?> <IPDetails> <productName>IBM Tivoli Workload Scheduler for Applications</product +Name> <vendorName>IBM</vendorName> <version>8.6.0.0</version> </IPDetails>`

Output XML should be:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Inventory PUBLIC "Discovery" "http://www.company.com/Dis +covery.dtd"> <Discovery Tracker="9.3" Audience="machine" Scope="rhel6x86sriram" + MachineName="rhel6x86sriram" UserName="system" Hardware="True" Softw +are="True" TrackFilesInUserDiscovery="True" DateTime="20140212T124338 +" Type="Full" FileScan="True"> <Package Name="IBM Tivoli Workload Scheduler for Applications" Evi +dence="IP" Version="8.6" InstallDate="20140115T085823"> <Property Name="InstallLocation" Value=""/> <Property Name="Publisher" Value="IBM"/> </Package> </Discovery>

I am new to xml and i am confused whether to use XML::Simple or XML::LibXML.In the output xml,Discovery node attributes should be hardcoded. Please provide the right approach to produce the output xml file. Please give me the pseudocode if possible.

Your help is much appreciated.

Sriram

Replies are listed 'Best First'.
Re: How to use perl XML::LibXML Parser?
by tangent (Parson) on Feb 26, 2014 at 12:53 UTC
    Here is an example using XML::LibXML. There are many ways to use the module but I find that Xpath expressions are really powerful once you get your head around them.
    use XML::LibXML; my $string = q|<?xml version="1.0"?> <IPDetails> <productName>IBM Tivoli Workload Scheduler for Applications</product +Name> <vendorName>IBM</vendorName> <version>8.6.0.0</version> </IPDetails>|; my $tree = XML::LibXML->load_xml(string => $string); open (my $out_xml,">",'out.xml') or die "with nice message: $!"; print $out_xml qq|All the hardcoded stuff\n|; my @nodes = $tree->findnodes('/IPDetails'); for my $node (@nodes) { my $product_name = $node->findvalue('productName'); my $vendor_name = $node->findvalue('vendorName'); my $version = $node->findvalue('version'); print $out_xml qq|<Package Name="$product_name" Version="$version" +>\n|; print $out_xml qq|<Property Name="Publisher" Value="$vendor_name"> +\n|; print $out_xml qq|</Package>\n|; } print $out_xml qq|End stuff\n|; close $out_xml;

      Thanks tangent for your sample script. I liked it and however i used LibXML->parse_file method to parse the xml and used it's object to fetch the nodes and their values using text content method.

      Regards,

      Sriram

Re: How to use perl XML::LibXML Parser?
by ww (Archbishop) on Feb 26, 2014 at 12:54 UTC

    "Gimmé, gimmé, gimmé!"

    OK, here's a step by step approach:
    • Read docs
      Comprehend
      Select a module
    • Consider approaches to your problem (how would you do it with paper and pencil
    • Use prior steps to guide your attempt to pseudocode your problem
    • Try to code in Perl using your pseudocode (and other docs, as needed)
    • Come back with any remaining problems, error messages and/or warnings and a narrative description of why your output is unsatisfactory.
    Come, let us reason together: Spirit of the Monastery
Re: How to use perl XML::LibXML Parser?
by sundialsvc4 (Abbot) on Feb 26, 2014 at 13:18 UTC

    Also, particularly in this case, consider all of the technological alternatives that you have to do this very-common task, which is:   transforming one XML document into another XML document.   There are existing technologies called XSLT and XQuery which are specifically designed to do this ... without writing a computer-program in any language to do it.

    Before you do much of anything else on this project, Google terms such as saxon, xalan, libxslt, and MSXML.   Of course Perl has support for it, too, e.g. in XML::LibXSLT, but it may well be that to do this task you can ... well, maybe you ca do it in your web-browser, word processor, or spreadsheet!

    For a striking example of what I am talking about, go to http://excelhero.com/periodic-table/.   This entire display is built using XSLT transforms that are taking place in your browser.   There are only a couple of short JavaScript routines to provide the interation.   No programming is used to build the display.

      Regarding http://excelhero.com/periodic-table/ WOW!

      Jason L. Froebe

      Blog, Tech Blog

        Uh huh ... that’s exactly my reaction, too.

        Then, when you start peeking at the source-code of the thing, you discover an XSLT document, and a small one at that, which is drawing data from XML data-sources.   Included in that output is just a couple of very small JavaScript onclick and mouseover routines, just for effect.   The HTML that you see is generated, and it’s being done by your browser!

        Another, much more inaccessible, example of XSLT is DocBook, which is the technology that enabled all those O’Reilly books (the ones with animals on the cover ...) to be produced in all those different forms.   You suspected that the same material was being drawn-from over and over again, and so it was.   You suspected that the extremely-regular formatting of the books was generated, somehow.   And it was, and this is how.   Not through programming, but through XSLT.   Online help, slide shows, all sorts of things, are produced this way.

        (And I’ve done a lot of it ... happy to consult.)