Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Perl XML

by ramya2005 (Scribe)
on Aug 09, 2005 at 23:22 UTC ( [id://482466]=note: print w/replies, xml ) Need Help??


in reply to Re: PERL XML
in thread Perl XML

I can't use XML::Simple because it is not preserving attributes and elements.
Also for my application, I have to preserve the order and structure of the XML that I am creating.
Regarding my requirement, I should be able to parse the XML file going step by step from top to bottom.
While parsing I should be able to edit some of the tags and so on. When I finish I should be able to create an XML which is similar to the original one in structure.

Replies are listed 'Best First'.
Re^3: PERL XML
by Joost (Canon) on Aug 09, 2005 at 23:32 UTC
    I tend to use XML::Twig for most of my perl/XML work, but most "full featured" XML parsers should be able to deal with your data provided it's presented as actual XML (your <img> tag is missing an ending slash, so your input is not valid).

    If you have valid XML input, XML::Parser is probably one of the most flexible solutions, but IMO most problems can be solved easier with either XML::Twig or even XML::Simple. (See also the ForceArray and ForceContent options of XML::Simple)

    update: ignore the parts I striked - I was replying to 2 different XML threads at the same time, and I got confused. Luckily, most of the advise is reasonably sound. :-)

      Thanks for your response. I would appreciate if you can provide me with a more specific example or code snippet using XMl::Twig

        I'm not going to write the code for you, but I did have a similar requirement that a particular element could exist prior to me trying to insert it. So I wrote a quick "xml_child" routine that would look for a specific element, return it if it were there, but if it weren't, it would create it, then return that. You can also provide parms for setting some stuff if the element is being created, or other parms for setting stuff whether it's being created or updated.

        Note that this was my first time using XML of any type, let alone XML::Twig, so be gentle with any criticisms ;-)

        Example usage:

        $child = xml_child(parent => $elt, child => 'built', action => 'after' +, relto => $prev_child, def_text => '19700101 000000.000-0000'); $prev_child = $child; $child = xml_child(parent => $elt, child => 'sha +reable', action => 'after', relto => $prev_child, override_text => 't +rue');
        Here I'm creating a series of elements, and even specifying order by the order I insert (although if it's already there out of order, then I won't correct the order). I also have some default text which won't change anything that is already present but I have some override text that says that I don't care what the XML owner says, I'm changing the shareable element to be "true".

        This is probably adaptable to what you sound like you need.

        If you're wondering why the lack of whitespace in the example - it's because I have about 30 of these... and I just needed to get it done - after it was done, it was more important to be able to pgup/pgdn through past these constants than to make it readable. ;-> If I were doing it again, I'd find a way to put this data somewhere else and loop through it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found