Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

From a folder, read all the XML files and add the end tag if not present.

by Anonymous Monk
on Nov 15, 2012 at 09:06 UTC ( [id://1003968]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I have a question for you, Pls can you suggest a solution for this...?

In a folder I have some 25 XML files . In some XML files the end tag is missing, so that it is not validating. I need to check whether it is having the end tag and if it is not there need to add it.

The sample XML file is below, and in some cases it is missing the last </main> tag. I need to check whether it is present there and if not there add it...

<main> <tag1> </tag1> <tag2> </tag2> <tag3> </tag3> </main>

Any possibilities Monks? Hope you can help(Also note that I am not a Perl programmer!) Thanks in advance!

  • Comment on From a folder, read all the XML files and add the end tag if not present.
  • Download Code

Replies are listed 'Best First'.
Re: From a folder, read all the XML files and add the end tag if not present.
by tobyink (Canon) on Nov 15, 2012 at 09:13 UTC

    It is a quick check and a quick fix with a text editor, and there are only 25 files.

    Sometimes it's easier to just do it manually.

    That said, if you have the xmllint command-line tool available (which is fairly standard, and packaged by almost every Linux distribution), then that should be able to tell you very quickly which files have errors:

    xmllint --noout *.xml
    
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thanks tobyink, but there is more than 25 files (around 300 is there and cant do it manualy, I just mentioned 25 as a number.) Also I am working on windows, so I cannot use 'xmllint'. Pls can you suggest with a module or a program?

        Also I am working on windows, so I cannot use 'xmllint'

        Sure you can. Don't go looking to reinvent xmllint, go looking how to install xmllint

Re: From a folder, read all the XML files and add the end tag if not present.
by Anonymous Monk on Nov 15, 2012 at 09:20 UTC
Re: From a folder, read all the XML files and add the end tag if not present.
by Anonymous Monk on Nov 15, 2012 at 14:14 UTC
    You know that end-tags are missing .. which is highly unusual, BTW .. but what else might be wrong with the files? Be careful that you do not try too hard to "fix" incorrect inputs. There is the perhaps apocryphal tale of a COBOL compiler that "compiled" a copy of Lincoln's Gettysburg Address with no "errors."

Log In?
Username:
Password:

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

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

    No recent polls found