Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

XML issue

by danielcid (Scribe)
on Jul 22, 2004 at 14:41 UTC ( #376598=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I hope you can help me with this issue.

I have a XML file:
<type name="bla"> <display>BLA BLA</display> <data> <item>FIRST</item> <item>SECOND</item> </data> <config> <field="test" name="test1"> <display>MY TEST</display> </field> </config> </type>

We are using XML::Smart to parse it, but now I need
to print some parts of the XML (the config one)
unparsed. I mean, I need to print only:
<config> <field="test" name="test1"> <display>MY TEST</display> </field> </config>

Is it possible to do it with XML::Smart ?
I can't modify the XML file to add a CDATA (it's from a
third part). Am I clear ? Thanks for your help..

Replies are listed 'Best First'.
Re: XML issue
by gellyfish (Monsignor) on Jul 22, 2004 at 16:44 UTC

    One way of doing this would be (Ihave fixed your XML BTW):

    use XML::DOM; my $xml =<<EX; <type name="bla"> <display>BLA BLA</display> <data> <item>FIRST</item> <item>SECOND</item> </data> <config> <field id="test" name="test1"> <display>MY TEST</display> </field> </config> </type> EX + my $parser = new XML::DOM::Parser; my $doc = $parser->parse($xml); + my $nodes = $doc->getElementsByTagName ("config"); + foreach my $node (@{$nodes}) { print $node->toString(); }
    Of course this is not using XML::Smart - but I had issues installing it and gave up.


      What issues installing it, since it doesn't have dependencies?

      Graciliano M. P.
      "Creativity is the expression of the liberty".

        You mean other than the Object::MultiType?


Re: XML issue
by jeffa (Bishop) on Jul 22, 2004 at 21:30 UTC

    TIMTOWTDI via XML::XPath

    use XML::XPath; use Data::Dumper; my $xpath = XML::XPath->new(ioref => *DATA); my $nodeset = $xpath->find('/type/config'); print map $_->toString, $nodeset->get_nodelist; __DATA__ <type name="bla"> <display>BLA BLA</display> <data> <item>FIRST</item> <item>SECOND</item> </data> <config> <field id="test" name="test1"> <display>MY TEST</display> </field> </config> </type>


    (the triplet paradiddle with high-hat)
Re: XML issue
by Scarborough (Hermit) on Jul 22, 2004 at 15:55 UTC
    If this is the real XML its not properly formated the field tag is incorrect.

    Anyway I use XML::Simple for a job like this. I read the XML in like so.
    $hash = XMLin($file);
    Then I mess around picking out the keys and values I need into a second hash then I use
     $txt = XMLout(%hash2)
    to get the new XML. Works for me but I'm interesred in better ways of doing this as I'm working on a project now which does something similer.
Re: XML issue
by gmpassos (Priest) on Jul 22, 2004 at 21:35 UTC
    See the method data_pointer() of XML::Smart:
    print $XML->{type}{config}->data_pointer() ;
    Note, always get the last version of XML::Smart!

    Graciliano M. P.
    "Creativity is the expression of the liberty".

      It helped! Thank you very much. Btw, Congrats for
      the work at XML::Smart. Its an excellent module!
      God bless CPAN and the people who help improve it :)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://376598]
Approved by Limbic~Region
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-10-24 01:29 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (242 votes). Check out past polls.