Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Parsing Data from XML Schemas

by meetraz (Hermit)
on Jun 16, 2004 at 16:52 UTC ( [id://367302]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Parsing Data from XML Schemas
in thread Parsing Data from XML Schemas

In this case, it's an XML file with the database schema and rowset data embedded within it. If you look at the XML file, it should have <s:Schema> and <rs:data> sections.

Try something like this.. I've tested it and it works. Note that XML::Simple reads the entire XML file into memory and therefore is not ideal for large datasets.

use strict; use XML::Simple; my $xs = XML::Simple->new(); my $sqlref = $xs->XMLin('sqldata.xml'); my $rowsref = $sqlref->{'rs:data'}{'z:row'}; foreach my $row (@$rowsref) { print "---------New Row--------\n"; print $row->{'column1'}, "\n"; print $row->{'column2'}, "\n"; print $row->{'column3'}, "\n"; }

Replies are listed 'Best First'.
Re^4: Parsing Data from XML Schemas
by mosiondz (Acolyte) on Jun 16, 2004 at 23:06 UTC

    Thanks for the code example, meetraz! However, I receive "Not an ARRAY reference" on the following line:

    foreach my $row (@$rowsref) {

    I'm not sure why I'd be receiving this error, as everything seems to assigned correctly.

      Is there any way you can post a sample of the file you're trying to parse?
        Sure:
        - <xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="u +uid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-micro +soft-com:rowset" xmlns:z="#RowsetSchema"> - <s:Schema id="RowsetSchema"> - <s:ElementType name="row" content="eltOnly" rs:CommandTimeout="30"> - <s:AttributeType name="id" rs:number="1"> <s:datatype dt:type="int" dt:maxLength="4" rs:precision="10" rs:fixe +dlength="true" rs:maybenull="false" /> </s:AttributeType> - <s:AttributeType name="title" rs:number="2" rs:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="132" rs:m +aybenull="false" /> </s:AttributeType> - <s:AttributeType name="description" rs:number="3" rs:nullable="true" + rs:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="1000" /> </s:AttributeType> - <s:AttributeType name="town" rs:number="4" rs:nullable="true" rs:wri +teunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="32" /> </s:AttributeType> - <s:AttributeType name="keywords" rs:number="5" rs:nullable="true" rs +:writeunknown="true"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="255" /> </s:AttributeType> - <s:AttributeType name="start_date" rs:number="6" rs:nullable="true" +rs:writeunknown="true"> <s:datatype dt:type="dateTime" rs:dbtype="timestamp" dt:maxLength="1 +6" rs:scale="3" rs:precision="23" rs:fixedlength="true" /> </s:AttributeType> - <s:AttributeType name="end_date" rs:number="7" rs:nullable="true" rs +:writeunknown="true"> <s:datatype dt:type="dateTime" rs:dbtype="timestamp" dt:maxLength="1 +6" rs:scale="3" rs:precision="23" rs:fixedlength="true" /> </s:AttributeType> - <s:AttributeType name="category" rs:number="8" rs:writeunknown="true +"> <s:datatype dt:type="string" rs:dbtype="str" dt:maxLength="60" rs:ma +ybenull="false" /> </s:AttributeType> <s:extends type="rs:rowbase" /> </s:ElementType> </s:Schema> - <rs:data> <z:row id="1099" title="Stones and Bones" description="Come and cons +ult with a specialist about the artifact or projectile point you have + always wondered about. Archeologists will be explaining their role i +n helping to unearth the past." town="Fort Macleod" start_date="2004- +08-09T00:00:00" end_date="2004-08-09T00:00:00" category="Event" /> <z:row id="1862" title="Whoop-Up Days Summer Fair & Rodeo" descripti +on="A midway, professional rodeo, rides, free entertainment, concerts +, trade show, Hobby World, fireworks, and tons of great food await yo +u at Lethbridge's own Whoop-Up Days. All of the above is included in +the "All in One" gate admission price of $8.00. Bring the family for +five days of great entertainment." town="Lethbridge" keywords="Whoop- +Up Days Summer Fair & Rodeo" start_date="2004-08-24T00:00:00" end_dat +e="2004-08-28T00:00:00" category="Event" /> <z:row id="2964" title="Remington Museum Summer Day Camps" descripti +on="These daylong camps take place from 9:00 am-4:00 pm and offer a v +ariety of fun and educational opportunities for children ages 6 throu +gh 12." town="Cardston" start_date="2004-07-21T00:00:00" end_date="20 +04-07-23T00:00:00" category="Event" /> <z:row id="3776" title="Shady Grove Bluegrass Music Festival" descri +ption="A special treat for all music lovers! Tap your toes to the swe +et sounds of some of the best country and bluegrass music in Canada." + town="Nanton" start_date="2004-08-20T00:00:00" end_date="2004-08-22T +00:00:00" category="Event" /> <z:row id="5070" title="Alberta Summer Games 2004" description="The +Alberta Games are a provincial competition, which provides a competit +ive opportunity for developing athletes, generally twelve to seventee +n years of age." town="High River" start_date="2004-07-22T00:00:00" e +nd_date="2004-07-25T00:00:00" category="Event" /> <z:row id="5180" title="Show N' Shine - Vulcan" description="A car s +how with antique cars, tractors, bikes, trucks, and more. Also a burn + out contest, a demolitian derby, and family fun!" town="Vulcan" star +t_date="2004-07-24T00:00:00" end_date="2004-07-24T00:00:00" category= +"Event" /> <z:row id="5264" title="World Indigenous Peoples Day" description="S +inging, drumming and dancing on this day links the Heritage Sites of +Indigenous Peoples in North and South America." town="Fort MacLeod" s +tart_date="2004-08-09T00:00:00" end_date="2004-08-09T00:00:00" catego +ry="Event" /> <z:row id="5427" title="Oldman River Antique Equipment and Threshing + Club Annual Show" description="Annual Threshing days" town="Pincher +Creek" start_date="2004-07-31T00:00:00" end_date="2004-08-01T00:00:00 +" category="Event" /> <z:row id="6828" title="100th Anniversary of Frank Slide" descriptio +n="Commemorative services at Frank Slide. Memorial, social and premie +r showing of new audio-visual presentation at the Frank Slide Interpr +etive Centre." town="Crowsnest Pass" start_date="2003-04-29T00:00:00" + end_date="2003-04-29T00:00:00" category="Event" /> <z:row id="7443" title="Stavely Annual Horse Show and Amateur Rodeo" + description="BBQ - 5PM, dance - 9:30PM, horseshow - 9AM, rodeo - 6:3 +0PM." town="Stavely" start_date="2004-08-28T00:00:00" end_date="2004- +08-28T00:00:00" category="Event" /> <z:row id="7931" title="Nanton FCA Rodeo" description="Hang on to yo +ur hats 'cuz here's 4 days of rodeoing that you'll never forget." tow +n="Nanton" start_date="2004-08-01T00:00:00" end_date="2004-08-02T00:0 +0:00" category="Event" /> <z:row id="7934" title="Nanton Nite Rodeo Three" description="This i +s our pride and joy at the Nanton Ag Grounds. What you see here is a +family event, with nothing but local talent. It's not all about winni +ng. Watch closely and you'll see adults help the children learn rodeo +ing as its meant to be." town="Nanton" start_date="2004-07-31T00:00:0 +0" end_date="2004-07-31T00:00:00" category="Event" /> <z:row id="7935" title="Nanton Nite Rodeo" description="This is our +pride and joy at the Nanton Ag Grounds. What you see here is a family + event, with nothing but local talent. It's not all about winning. Wa +tch closely and you'll see adults help the children learn rodeoing as + its meant to be." town="Nanton" start_date="2004-07-23T00:00:00" end +_date="2004-07-23T00:00:00" category="Event" /> <z:row id="7936" title="Nanton Nite Rodeo Five" description="This is + our pride and joy at the Nanton Ag Grounds. What you see here is a f +amily event, with nothing but local talent. It's not all about winnin +g. Watch closely and you'll see adults help the children learn rodeoi +ng as its meant to be." town="Nanton" start_date="2004-08-07T00:00:00 +" end_date="2004-08-07T00:00:00" category="Event" /> <z:row id="7937" title="Nanton Nite Rodeo Final" description="This i +s our pride and joy at the Nanton Ag Grounds. What you see here is a +family event, with nothing but local talent. It's not all about winni +ng. Watch closely and you'll see adults help the children learn rodeo +ing as its meant to be. 25th Anniversary Rodeo, with Past Champion's +Rodeo to follow" town="Nanton" start_date="2004-08-14T00:00:00" end_d +ate="2004-08-14T00:00:00" category="Event" /> <z:row id="8459" title="Bar U Ranch National Historic Site Canada Da +y" description="Thursday, July 1, 2004. The Bar U and the ranching hi +story we celebrate are an important part of Canada's national identit +y. On Canada Day you are invited to join in the celebration of our na +tion's birthday. Western art, a special flag raising, and, best of al +l, free admission for all are among the highlights of the day." town= +"Longview" start_date="2004-07-01T00:00:00" end_date="2004-07-01T00:0 +0:00" category="Event" /> <z:row id="8460" title="Polo at the Bar U Ranch National Historic Si +te" description="Sunday, August 1 help celebrate Polo's 100th anniver +sary at the Bar U Ranch. And what better way to celebrate than to re- +create the excitement of the Bar U's legendary Geebung Club with a th +rilling match on the Bar U's original polo field." town="Longview" st +art_date="2004-08-01T00:00:00" end_date="2004-08-01T00:00:00" categor +y="Event" /> <z:row id="8464" title="Bar U Ranch National Historic Site Women of +Ranching" description="Sunday, August 8, 2004. The Bar U's popular Ro +und up Camp is the place to be as we celebrate the lives and stories +of the women in ranching. Local gals of all ages will gather around t +he campfire to share their heartwarming tales--of life and home on th +e range." town="Longview" start_date="2004-08-08T00:00:00" end_date=" +2004-08-08T00:00:00" category="Event" /> <z:row id="8465" title="Bar U Ranch National Historic Site Legends o +f Rodeo" description="Sunday, September 5, 2004. The crackling fire o +f Roundup Camp is the place to be as we spend an afternoon celebratin +g rodeo and its stars of yesteryear. Their stories may tickle your fu +nnybone or breal your heart but they will be sure to entertain." town +="Longview" start_date="2004-09-05T00:00:00" end_date="2004-09-05T00: +00:00" category="Event" /> <z:row id="8900" title="13th Annual Elks Club Chili Cook Off" descri +ption="Looking for an event to wet your taste buds? The Elks Annual C +hili Cook Off provides some of the best chili around. So come and exp +erience chili the way it was meant to be eaten - off an authentic chu +ckwagon." town="Okotoks" start_date="2004-09-04T00:00:00" end_date="2 +004-09-06T00:00:00" category="Event" /> </rs:data> </xml>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://367302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-25 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found