perl - read xml file without any XML module -
i trying read xml form using perl can not use xml modules xml::simple, xml::parse.
it simple xml form has basic information , ms doc attachment. want read xml , download attached doc file print xml information in screen.
but don't know way how can without xml module, heard xml file can parse using data::dumper not familiar module, not getting how this.
could please me on if there way without xml modules?
sample xml:
<?xml version="1.0"?> <catalog> <book id="bk101"> <author>gambardella, matthew</author> <title>xml developer's guide</title> <genre>computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>an in-depth @ creating applications xml.</description> </book> <book id="bk102"> <author>ralls, kim</author> <title>midnight rain</title> <genre>fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>a former architect battles corporate zombies, evil sorceress, , own childhood become queen of world.</description> </book> </catalog>
i'd re-iterate bad idea. because whilst xml looks plain text - it's isn't plain text. , if treat such, creating brittle, unmaintainable , unsupportable code, may break 1 day, because changes xml format in valid way.
i suggest first port of call go project, , point out how parsing xml without xml parser rather trying use hammer put screws piece of wood. in sort of works, results rather shoddy, , frankly it's unnecessary because screwdrivers exist , job properly, , available.
e.g.
can tell me how can print author, title , price each book id above xml file xml module ?
#!/usr/bin/env perl use strict; use warnings; use xml::twig; $twig = xml::twig -> new -> parsefile ( 'your_file.xml' ); foreach $book ( $twig -> get_xpath ( '//book' ) ) { print join ("\n", $book -> att('id'), $book -> field('author'), $book -> field('title'), $book -> field('price'), ),"\n----\n"; }
however:
given very specific sample, may able away treating 'plain text'. before this, should point out project lead risky approach - you're putting in screws hammer - , therefore creating ongoing risk of support problems, trivially resolved installing bit of freely available, open source code.
i suggesting at all because i've had deal ludicrously unreasonable similar project demands.
like this:
#!/usr/bin/env perl use strict; use warnings; while ( <> ) { if ( m/<book/ ) { ( $id ) = ( m/id="(\w+)"/ ); print $id,"\n"; } if ( m/<author/ ) { ( $author ) = ( m/>(.*)</ ); print $author,"\n"; } }
now, reason doesn't work sample above can validly formatted as:
<?xml version="1.0"?> <catalog><book id="bk101"><author>gambardella, matthew</author><title>xml developer's guide</title><genre>computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>an in-depth @ creating applications xml.</description></book><book id="bk102"><author>ralls, kim</author><title>midnight rain</title><genre>fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>a former architect battles corporate zombies, evil sorceress, , own childhood become queen of world.</description></book></catalog>
or
<?xml version="1.0"?> <catalog> <book id="bk101"> <author>gambardella, matthew</author> <title>xml developer's guide</title> <genre>computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>an in-depth @ creating applications xml.</description> </book> <book id="bk102"> <author>ralls, kim</author> <title>midnight rain</title> <genre>fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>a former architect battles corporate zombies, evil sorceress, , own childhood become queen of world.</description> </book> </catalog>
or:
<?xml version="1.0"?> <catalog ><book id="bk101" ><author >gambardella, matthew</author><title >xml developer's guide</title><genre >computer</genre><price >44.95</price><publish_date >2000-10-01</publish_date><description >an in-depth @ creating applications xml.</description></book><book id="bk102" ><author >ralls, kim</author><title >midnight rain</title><genre >fantasy</genre><price >5.95</price><publish_date >2000-12-16</publish_date><description >a former architect battles corporate zombies, evil sorceress, , own childhood become queen of world.</description></book></catalog>
or:
<?xml version="1.0"?> <catalog> <book id="bk101"><author>gambardella, matthew</author><title>xml developer's guide</title><genre>computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>an in-depth @ creating applications xml.</description></book> <book id="bk102"><author>ralls, kim</author><title>midnight rain</title><genre>fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>a former architect battles corporate zombies, evil sorceress, , own childhood become queen of world.</description></book> </catalog>
this why have many comments 'use parser' - snippets above, simplistic example gave you... work on 1 , break messily on others.
but xml::twig
solution handles them correctly. xml::twig
freely available on cpan. (there's other libraries job well). , it's pre-packaged lot of operating systems 'default' repositories.
Comments
Post a Comment