I use perl a lot for "getting the job done." I'm in a corner now where I have to use perl for a production-y tool that calls an XML REST API. Normally for perl and XML I make do with XML::Simple. I know it is discouraged, but is *is* simple and gets the job done.

Now I need to read an XML document, modify it, and POST it back to that REST API compliant with an XML Schema. XML::Simple won't cut it -- too many ways it will write the XML out differently than the input. So I have turned to XML::Compile and XML::LibXML. I've got it working, but I think it can work better for my purpose.

I am loading my schema with XML::Compile and and I am reading in my XML document with a READER. I end up with a reference variable to XML::LibXML::Element. I am able to use method calls like ->addChild to do to this structure what I need to do, but then I found this blog post which shows me how I can construct a hash of hashes not dissimilar to what XML::Simple gives me. I am much more comfortable manipulating this kind of structure. The blog post shows how to get an "empty" template, fill it out, and use that structure to generate an XML document. This is great, but for my use case I don't want a completely empty structure where I have to populate every tag. I want a structure populated with values from an input XML document.

In other words, I have done a GET from my REST API and I have an XML document in a string compliant with my Schema (which I have successfully built with XML::Compile). Now instead of using the READER to get perl objects where I have to use methods to access and change the contents, I want to get the same kind of structure as the example from the blog:

warn $schema->template('PERL', 'addresses');

But instead of an empty structure, I want it initialized with the data from my XML string. Can anyone show me the way to do that? I am just NOT finding it...


In reply to XML::Compile template initialized from XML document by tdane

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.