http://www.perlmonks.org?node_id=1002032


in reply to Re: XML Parse Change a Value and Write Back
in thread XML Parse Change a Value and Write Back

The whole Output XML goes into a mess. I just want the output to look how my input is. Isn't there any other choice?
  • Comment on Re^2: XML Parse Change a Value and Write Back

Replies are listed 'Best First'.
Re^3: XML Parse Change a Value and Write Back
by bart (Canon) on Nov 02, 2012 at 19:47 UTC
    Don't use XML::Simple.

    There's ,no easy solution, I think, because there's probably no XML module around that guarantees that the produced attributes are in the same order as they are parsed.

    But XML::Simple guarantees almost absolutely nothing, not even that the output will have a similar structure as the input. It may even move data from content to attribute or vice versa. the only guarantee you get on the output of XML::Simple is that XML::Simple will be able to read it back in.

      You might get by with XML::Treebuilder. You build a tree from the original XML, and use XML::Element to find where you want to insert new elements and insert them, then dump the tree back to a file with the appropriate pretty printing. If I have time later I'll try to post a simple example. It's not as simple as XML::Simple, but it's not as much to wade through as XML::LibXML.
        Treebuilder is again good for parsing and writing the xml but its very difficult to find the node and modify a parameter value.
Re^3: XML Parse Change a Value and Write Back
by Arunbear (Prior) on Nov 02, 2012 at 23:55 UTC
    Why not read the post that I linked to? Two other methods were mentioned: 1) XML::Twig 2) search and replace using a regular expression.
      XML Twig is so confusing. Is there any post which explains xml twig in detail? Regexp i cannot use, the parameters are determined by nodes and thus it fails. Can you suggest some good and easy to use module. I have tried hell lot of modules some are good parsers and some are good modifiers but i am unable to find a module which is a good parser as well writer of XML. Be notified I cannot afford to change my output xml parameters locations.
Re^3: XML Parse Change a Value and Write Back
by BrowserUk (Patriarch) on Nov 03, 2012 at 11:51 UTC
    I just want the output to look how my input is.

    What goes wrong with your subsequent processing if the syntactic layout of your XML changes even though the semantic layout is retained?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong