Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How do I ignore comments in an xml file when using win32::ole?

by ketanh (Novice)
on Jun 25, 2011 at 14:14 UTC ( #911375=perlquestion: print w/ replies, xml ) Need Help??
ketanh has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to parse the following xml file. It works for the most part, but bails out with an error when it hits the xml comments

<?xml version="1.0" encoding="UTF-8"?> <Header> <Main id="1" name="Item1"> </Main> <Main id="2" name="Item2"> </Main> <!--Main id="3" name="Item4"> </Main--> <Main id="4" name="Item5"> </Main> </Header>

This is my perl script I'm using to parse

use Win32::OLE qw(in with); my $xml_file = './test.xml'; my $dom = Win32::OLE->new('MSXML2.DOMDocument') or die "new() failed"; $dom->{async} = "False"; $dom->{validateOnParse} = "False"; $dom->Load($xml_file) or die "Parse failed"; my $Header = $dom->DocumentElement(); my $Mains = $Header->childNodes(); foreach my $main(in $Mains) { my $main_id = $main->Attributes->getNamedItem("id")->Text; my $main_name = $main->Attributes->getNamedItem("name")->Text; print "$main_id,$main_name\n"; }

This is the output it gives me, how do I make it ignore the sections that are commented out?
1,Item1
2,Item2
Can't call method "getNamedItem" on an undefined value at xml_try.pl line 15.

Comment on How do I ignore comments in an xml file when using win32::ole?
Select or Download Code
Re: How do I ignore comments in an xml file when using win32::ole?
by choroba (Canon) on Jun 25, 2011 at 15:20 UTC
Re: How do I ignore comments in an xml file when using win32::ole?
by AnomalousMonk (Abbot) on Jun 25, 2011 at 17:31 UTC

    Correct me if I'm wrong (and I may very well be wrong for I have #$%&-all experience with XML or XML parsing), but it seems to me that
        <!--Main id="3" name="Item4">
    is not a proper XML comment and so maybe Win32::OLE is just choking and returning an undefined value when it encounters something that is, in fact, 'undefined' qua XML. (Would not
        <!--Main id="3" name="Item4"-->
    be a proper comment: see '-->' at end?)

    Just curious.

Re: How do I ignore comments in an xml file when using win32::ole?
by toolic (Bishop) on Jun 25, 2011 at 17:53 UTC
    Here is a Twig::XML solution:
    use warnings; use strict; use XML::Twig; my $str = <<EOF; <?xml version="1.0" encoding="UTF-8"?> <Header> <Main id="1" name="Item1"> </Main> <Main id="2" name="Item2"> </Main> <!--Main id="3" name="Item4"> </Main--> <Main id="4" name="Item5"> </Main> </Header> EOF my $t = XML::Twig->new(twig_handlers => { Main => sub {print $_->att('id'), "\n"} }); $t->parse($str); __END__ 1 2 4

      @choroba, I'm a mere systems engineer trying to parse xml :) I've used xml::simple before, but that doesn't work with the file I have now. So I went with the search that gave me the easiest example to read and follow.

      @toolic, Thanks for the example. I didn't know what to do with the xml that you added into the code. I tried reading it in a few ways, didn't help. I have to read a file that I download from a repository and have to load it into my parser
      However, thanks to your example, I explored XML::TWIG more and figured out a way to do this.
      This link was very helpful
      http://www.xml.com/pub/a/2001/04/18/perlxmlqstart1.html

      use warnings; use strict; use XML::Twig; my $file = './test.xml'; my $twig = XML::Twig->new(); $twig->parsefile($file); my $root = $twig->root; foreach my $item ($root->children('Main')){ print $item->att('id').", ".$item->att('name'); print "\n"; }

      This gave me the desired output.
      1, Item1
      2, Item2
      4, Item5

      @AnomalousMonk, I won't claim to be any sort of expert with xml. To me, if internet explorer grey'ed it out, and Visual SlickEdit "green"ed it out, I take it it's a comment in the XML. :)

      Thanks for all your help!

        I didn't know what to do with the xml that you added into the code. I tried reading it in a few ways, didn't help. I have to read a file that I download from a repository and have to load it into my parser
        XML::Twig is flexible in that it allows you to parse either a file (as you have done) or a Perl scalar variable (as I have done). Using the latter is merely for the convenience of creating a small self-contained code sample.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2015-07-04 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls