Reputation: 55
I'm new to Erlang and am trying to parse an XML document into a record. I have tried various methods (xmerl_scan/xpath) but get painted into a corner because I don't know enough to know where to begin.
I've plodded through three books on the subject and spent a few hours googling the subject matter but I think I must be missing something obvious.
There must be a simple way to visit each node using some sort of pattern matching and generate a list of matches.
This is the input...
<Sports>
<Sport> SportId="1" SportName="Cricket">
<League LeagueId="1" LeagueName="Test Match">
<Meeting MeetingId="1" MeetingName="Test Match"/>
</League>
<League LeagueId="2" LeagueName="One Day International">
<Meeting MeetingId="1" MeetingName="One Day International Match""/>
<Meeting MeetingId="2" MeetingName="One Day International Series"/>
</League>
<League LeagueId="3" LeagueName="T20 World Cup">
<Meeting MeetingId="1" MeetingName="T20 World Cup Futures"/>
</League>
</Sport>
<Sport SportId="7" SportName="Golf">
<League LeagueId="1" LeagueName="PGA Tour">
<Meeting MeetingId="1" MeetingName="St Jude Classic"/>
<Meeting MeetingId="2" MeetingName="US Open"/>
</League>
<League LeagueId="22" LeagueName="European Tour">
<Meeting MeetingId="100" MeetingName="Nordea Masters"/>
<Meeting MeetingId="101" MeetingName="Nordea Masters Tournament HH"/>
<Meeting MeetingId="102" MeetingName="Nordea Masters Top Aussie"/>
<Meeting MeetingId="107" MeetingName="Nordea Masters Handicap"/>
<Meeting MeetingId="110" MeetingName="Nordea Masters R1 3 Balls"/>
</League>
<League LeagueId="34" LeagueName="Ryder Cup">
<Meeting MeetingId="1" MeetingName="Ryder Cup 2012"/>
</League>
</Sport>
</Sports>
and this is the desired output...
[{SportId,SportName,LeagueId,LeagueName,MeetingId,MeetingName}]
Don't necessarily need a canned answer but just a push in the right direction would be useful so I can do some further research myself.
I've used mochiweb_html:parse to search for a specific information in a particular node but in the above instance I need to extract information from multiple nodes.
thanks
Upvotes: 2
Views: 1958
Reputation: 2005
Have you looked into erlsom yet?
It is IMO easier to use than xmerl. With erlsom you can generate record definitions from an XSD schema. The record definitions map the types from the schema (plus some extra information). Having the records pattern matching is straight forward to use. More info you can find here.
Upvotes: 1