CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Deleting empty tag of an XML file

 



perlmagix
Novice

May 13, 2016, 4:48 AM

Post #1 of 4 (2281 views)
Deleting empty tag of an XML file Can't Post

In the following test.xml file,


Quote
<one>abcfd erer 123</one>
<two></two>
<three>adew 123dsaads</three>


To delete the tag <two> with no data,

the following command is helpful,


Code
`sed '/><\//d' test.xml`


In a similar way, for the following xml file,


Quote
<one>abcfd erer 123</one>
<two>
</two>
<three>adew 123dsaads</three>


which command would be helpful to delete the tag <two> !


BillKSmith
Veteran

May 13, 2016, 7:36 AM

Post #2 of 4 (2276 views)
Re: [perlmagix] Deleting empty tag of an XML file [In reply to] Can't Post

In your previous post, I explained that the only reliable way to edit XML is to first parse it with a module from CPAN. The fact that you are already working on a similar problem should convince you that it is worth the effort to learn how. Your new question demonstrates the problem with "do it yourself" solutions. You encounter special cases where they do not quite work. Even if you devise a work around, you are still likely to find another special case, and then another and...
Good Luck,
Bill


cognizant
Novice

May 19, 2016, 9:57 PM

Post #3 of 4 (2232 views)
Re: [perlmagix] Deleting empty tag of an XML file [In reply to] Can't Post

Try this,

$data =~ s{\s*<two>\s*</two>\s*}{}g;

Staying with modules is beneficial while working with XML documents.

Cheers


Laurent_R
Veteran / Moderator

May 19, 2016, 11:29 PM

Post #4 of 4 (2230 views)
Re: [cognizant] Deleting empty tag of an XML file [In reply to] Can't Post

This won't work for many cases. To start with, it won't work if the tags are <one> and </one>. In fact, you have to make a long series of assumptions about the input format and content and the way to process this data before you can hope this regex to work.

No, regex is the wrong approach here. Don't shoot a bullet into your own foot.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives