Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

XML issue

by danielcid (Scribe)
on Jul 22, 2004 at 14:41 UTC ( [id://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.

    /J\

      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?

        /J\

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>

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (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 :)

      -DBC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found