Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Operating on XML, or XML::Simple is too simple!

by graff (Chancellor)
on Aug 05, 2005 at 05:26 UTC ( [id://481095]=note: print w/replies, xml ) Need Help??


in reply to Operating on XML, or XML::Simple is too simple!

So have you looked at using plain old XML::Parser? Just because you find a module called "XML::Simple", this doesn't mean that the fundamental module it's based on (XML::Parser) has to be really complicated -- and in fact it really is not complicated.

Clear your mind of XML::Simple, forget whatever preconceptions have arisen about how you think your code should be written, then read the man page for XML::Parser and see if some different approach becomes clear. You might find that it will be easier to use, and an appropriate algorithm might become self-evident.

(Consider that the man page for XML::Parser is only about one third as long as the one for XML::Simple -- that suggests to me that the latter module may have been misnamed.)

Your goal is not clearly stated here... (Maybe there's a previous thread where you gave more detail?) What needs to be modified? In what sense should the output be in "exactly" the same format, given that changes of some sort need to be made? (I.e. what specific features of the input format need to be preserved?)

And are you sure you need to hold the whole xml stream in a Perl data structure? Are there complicated, hierarchical conditions that are needed to control the mods? Or can the changes be based on relatively localized criteria?

Whatever the task is, XML::Parser will probably be simpler than XML::Simple (which seems to have been tailored to certain types of tasks, and your task might not be among that set).

Other references cited in earlier replies are worth looking at as well -- but I think looking at the fundamental module first would be worthwhile.

update: Sorry, I think I may have misunderstood. You are actually looking for a way to take a bunch of new data (from some non-XML source, like a database or flat file) and push them out in the XML format shown in the example -- is that it? You want a template with placeholders where you can plug in fresh values.

Since you seem to have some repeating elements ("voice_recipient_list" contains one or more "voice_recipients", etc) it's likely that different inputs will have different quantities of these elements. You could try setting up a set of nested templates -- e.g. a template for "voice_recipient" that can be used any number of times to build the "voice_recipient_list", then a template for "voice_broadcast_info" that has a placeholder for the "voice_recipient_list", and so on. But on the whole, it would seem more like you are building up the output from pieces, rather than just tweaking the content of an existing XML stream.

  • Comment on Re: Operating on XML, or XML::Simple is too simple!

Replies are listed 'Best First'.
Re^2: Operating on XML, or XML::Simple is too simple!
by jdtoronto (Prior) on Aug 05, 2005 at 15:28 UTC
    Graff,
    update: Sorry, I think I may have misunderstood. You are actually looking for a way to take a bunch of new data (from some non-XML source, like a database or flat file) and push them out in the XML format shown in the example -- is that it? You want a template with placeholders where you can plug in fresh values.
    Pretty much hots it on the head.

    I don't care about the XML. To me XML is a way of structuring data that is yet another way of structuring data. The idea of XML::Simple converting the XML to a Perl structure which could be modified has worked well for me before. But sadly XML::Simple fails my simple test of simplicity. If I take some XML, parse it and then output it with no other processing, it should be the same.

    That being said. I moved on and asked the question here. I suppose what I really want is the HTML::Template version of XML! The data in the XML example is a job submission. Essentially: send these message files to these recipients. I have installed XML::Twig, but at first look it is daunting, but I suspect that if I perservere it will all work out in the end.

    Many thanks, John jdtoronto

      Would XML::Dumper be closer to what you are looking for? I would not let you read random XML, but it would let you read/write your data structures as XML.

        Mirod,

        I am not sure! I have read so many pages of XML documentation, module documentation, Perlmonks posts and article on the Web that I am beginning to be unsure about life itself!

        Fortunately I don't actually need to READ any XML. What I really need is to GENERATE XML in the very form that the sample documents came to me. My brain is exploding right now and I have found a devastatingly simple solution - trusty old HTML::Template!

        But I have no doubt that XML is going to become an all too regular part of my life, so I am going to need to do some learning. I have installed XML::Twig and read your tutorials online and I am impressed. When I have a little time I will work through some of the examples and no dount have more questions. THis is one of those days when I wish I could find someone who wanted me to design some hardware rather than write code!

        jdtoronto

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found