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

my $t= XML::Twig->new(); $t->parsefile("new.xml"); my $root= $t->root; my @para= $root->children(); # get the root's children foreach my $para (@para) { print $para->text()."\n"; }


The XML file contents are as below.
<root> <record> <id>1</id> <sched>QWERTY</sched> <filePath>D:/Book1.xls</filePath> <fString>bata</fString> </record> <record> <id>2</id> <sched>PANAMA</sched> <filePath>C:/test.doc</filePath> <fString>vata</fString> </record> </root>


The output which I get is as below :
1QWERTYD:/Book1.xlsbata
2PANAMAD:/test.docvata


I want the output as below(space or any delimiter) :
1 QWERTY D:/Book1.xls bata
2 PANAMA D:/test.doc vata

Replies are listed 'Best First'.
Re: XML::Twig output with delimiter
by balakrishnan (Monk) on Dec 09, 2008 at 13:12 UTC
    I think ,this would help you to get your need,
    my $t= XML::Twig->new( twig_handlers => { '_all_' => sub { $_[1]->suffix( " ");}} ); $t->parsefile("new.xml"); my $root= $t->root; my @para= $root->children(); # get the root's children foreach my $para (@para) { print $para->text()."\n"; }
      Yes it solved the problem to some extent but I want the result to be in a variable.

      I tried like this :

      my $content = sprintf $para->text()."\n";

      It doesn't seem to work.

        Once again... read the docs. perldoc -f sprintf. If the text of the element includes a % you won't get what you want.

        You don't need sprintf at all in this case. my $content = $para->text()."\n"; will do just fine.

        Yes it works that way.

        If there is any other way then tell me.
Re: XML::Twig output with delimiter
by mirod (Canon) on Dec 09, 2008 at 11:33 UTC

    To answer your question, try print join " ", map { $_->text } $para->children.

    That said, why call para the record elements?

      probably copy/paste from convenientstore's question
      If the XML file is large then memory problem might occur.
      So I want the loop in which the data will be extracted.

      Also I want only data present in specific tags.

        Read the docs about twig_handlers or twig_roots to see how to deal with large files. If you want the data from specific tags, then read the docs for field.

        To read the doc, perldoc XML::Twig or man XML::Twig or look at XML::Twig, or online doc.

Re: XML::Twig output with delimiter
by Anonymous Monk on Dec 09, 2008 at 10:40 UTC
    1 QWERTY D:/Book1.xls bata | |-record | | |-id | | | |-PCDATA: '1' | | |-sched | | | |-PCDATA: 'QWERTY' | | |-filePath | | | |-PCDATA: 'D:/Book1.xls' | | |-fString | | | |-PCDATA: 'bata' 2 PANAMA C:/test.doc vata | |-record | | |-id | | | |-PCDATA: '2' | | |-sched | | | |-PCDATA: 'PANAMA' | | |-filePath | | | |-PCDATA: 'C:/test.doc' | | |-fString | | | |-PCDATA: 'vata'
      In the code can I do like this :

      $content = print $pan->text, "\n";

      Actually I did like that but no data is coming in variable $content.
        Either you're a troll, or you need to read perlintro