Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml tags checker

Status
Not open for further replies.

rouge03

Programmer
May 5, 2003
16
PH
Hi!

Anybody here know how to check for valid xml tags/pairs?

I'm trying to create a program that will check if a particular tag has a start and end tag. If a tag doesn't have a start tag then it gets stripped (actually this is the easy part. =p). Tags written as <tags/> are okay.


Thanks in advance!
 
I installed XML::Mini after trying a different XML checker modules that installed XML::parser along with 40-50k other perl modules cpan thought it needed...

From the man page for XML::Mini I did this:

Code:
#!/usr/bin/perl -w
  use strict;
  use XML::Mini::Document;
# Create a new XML::Mini::Document
  my $newDoc = XML::Mini::Document->new();
# Creating XML can be done easily by using a hash ref:
  my $h = {'user' => {'name' => 'kordaff', 'job' => 'sales flunky'},};
  $newDoc->fromHash($h);
# output the XML
  open (FILE,">somefile.xml")or die "Miserable: $!\n";
  print FILE $newDoc->toString();

That produces somefile.xml with these contents:

Code:
<user>
  <name>
   kordaff
  </name>
  <job>
   sales flunky
  </job>
 </user>

Using this example from man XML::parser to parse that:

Code:
#!/usr/bin/perl -w
use strict;
use XML::Parser;
my $p1 = new XML::Parser(Style => "Debug");
print $p1->parsefile("somefile.xml");

I get this:

Code:
 \\ ()
user || #10;
user ||
user \\ ()
user name || #10;
user name ||    kordaff
user name || #10;
user name ||
user //
user || #10;
user ||
user \\ ()
user job || #10;
user job ||    sales flunky
user job || #10;
user job ||
user //
user || #10;
user ||
 //

And, if I delete the </job> ending tag, that XML::parser snippet produces this error to die on:

mismatched tag at line 7, column 3, byte 64 at /usr/lib/perl5/site_perl/5.8.7/i686-linux/XML/Parser.pm line 187

Without the Debug parameter, it just spits out that error line for the missing </job> tag.

The man page for XML::parser goes on to talk about the Subs style (instead of Debug). Subs styles sounds nicely powerful:

'Each time an element starts, a sub by that name in the
package specified by the Pkg option is called with the
same parameters that the Start handler gets called with.

Each time an element ends, a sub with that name appended
with an underscore ("_"), is called with the same
parameters that the End handler gets called with.'

I couldn't actually figure out from the man page for another module I looked at named XML::Checker how you'd check for valid tags with it, but it seemed like it should be possible =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top