py_201 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have a XML file with the following Syntax:
<root>
 <member id="1" name="member1">
    <ips>
    <ip1>
     <from></from>
     <to></to>
    </ip1>
    <ip2>
     <from></from>
     <to></to>
    </ip2>
   </ips>
 </member>
</root>

Now i need to dynamically add/remove some ip tag to an existing member tag in this XML or add/remove some member tag in this.
so my question is how can i edit an existing XML. Do we have some editor modules in perl for that?
Any help would be appreciated.

Replies are listed 'Best First'.
Re: Edit XML File in perl
by keszler (Priest) on Dec 08, 2009 at 05:19 UTC
      Thanks for your response.
      But what you have mentioned are Xml parsers (used to read a parser).
      But i need to edit a XML file. I dont want to read it using a parser and write it to some other file using a XML writer. as that would certaily take time.
      Do we have XML editors like i would read a XML and at a particular point add or remove the required tags.
        These can do that. For example:
        #!/usr/bin/perl use strict; use warnings; use XML::Smart; my $xmlstring = q{ <root> <member id="1" name="member1"> <ips> <ip1> <from></from> <to></to> </ip1> <ip2> <from></from> <to></to> </ip2> </ips> </member> </root>}; my $XML = XML::Smart->new($xmlstring); $XML->{root}{member}{ips}{ip1}{from} = 'I am IPS IP1 From'; print $XML->data(noheader => 1); __END__ <root> <member id="1" name="member1"> <ips> <ip1> <from>I am IPS IP1 From</from> <to/> </ip1> <ip2> <from/> <to/> </ip2> </ips> </member> </root>
Re: Edit XML File in perl
by FalseVinylShrub (Chaplain) on Dec 08, 2009 at 05:31 UTC

    Hi

    Was just about to post a response when I saw your clarification re: keszler. However, I'm still not quite sure what you mean by an editor... Do you mean you want to open the file for a human to change, or you want to change the file without reading the whole thing into memory?

    You can "edit" XML as a stream using some of the XML modules already mentioned by keszler. In particular I've used XML::Twig for large files. though I wouldn't say it was exactly fast. Depends how complex the changes are (you have to output to a tempfile and rename, as far as I know).

    FalseVinylShrub

    N.B. Please review and test code, and use at your own risk... If I answer a question, I would like to hear if and how you solved your problem.

Re: Edit XML File in perl
by roboticus (Chancellor) on Dec 08, 2009 at 15:35 UTC
    py_201:

    Assuming you're talking about using code to edit your XML file, if you try to avoid the XML parser part you'll only have to reinvent it yourself (poorly, as it's harder than you think to make an XML parser). Don't worry about how much time it takes: That's a premature optimization. Until you know it's too slow, don't worry about it. Also, there are some XML parsers available that are pretty fast, so it shouldn't be an issue anyway.

    But that's now why I'm writing. What I really wanted to do was suggest that you change your XML a bit. You're using tags <ip1>, <ip2>, etc., in your <ips> section. You should just use <ip> -- if you want to number them, then give them a number or name attribute like this: <ip id="1"> or have another sub-node to give it a name:

    <ips> <ip> <name>foo</name> <from></from> <to></to> </ip> </ips>

    There are several problems with dynamically changing your node tags. The most problematic is that any programs processing your XML documents are going to have to have some "magic" to interpret the document because you're trying to make a tag convey two pieces of information: (1) that this is an ip item, and (2) which ip item it is.

    ...roboticus