Subscribe Bookmark RSS Feed

XML Attribute Help

michaelgingras7

Community Trekker

Joined:

Aug 13, 2016

Hi,

 

I am trying to parse some XML and having quite a difficult time. In particular, trying to get the name of the stadium (AT&T Center) or the city and state. See below.

 

I am able to easily parse anything from the "event-metadata" element or the "site-metadata" element using "On Element()" with "XML Attr()". It seems to me that the stadium name should be an attribute of the element "name", but JMP doesn't seem to recognize it that way.

 

An online version of the XML is available here: http://showcase.xmlteam.com/open_rendition.php?id=2129

 

Any help would be much appreciated. Maybe there is a problem with the way it is marked up by the publisher?

 

Thanks

-Mike

 

<sports-event>
<event-metadata xmlns:fs="java.io.File" date-coverage-type="event" event-key="l.nba.com-e.896939" event-status="pre-event" date-coverage-value="l.nba.com-e.896939" start-date-time="20160114T200000-0500" time-certainty="certain" event-name="Regular Season"site-alignment="home">
<site>
<site-metadata site-key="l.nba.com-s.538102" capacity="18581">
<name full="AT&T Center"/>
<home-location city="San Antonio" state="Texas" country="USA"/>
</site-metadata>
<site-stats/>
</site>
</event-metadata>
<team id="l.nba.com-t.21">
<team-metadata xmlns:fs="java.io.File" team-key="l.nba.com-t.21" alignment="home">
<name first="San Antonio" last="Spurs"/>
</team-metadata>
<team-stats date-coverage-type="season-regular"/>
</team>
<team id="l.nba.com-t.11">
<team-metadata xmlns:fs="java.io.File" team-key="l.nba.com-t.11" alignment="away">
<name first="Cleveland" last="Cavaliers"/>
</team-metadata>
<team-stats date-coverage-type="season-regular"/>
</team>
</sports-event>
1 ACCEPTED SOLUTION

Accepted Solutions
Justin_Chilton

Joined:

Aug 27, 2015

Solution

It's possible that the ampersand character in 'AT&T Center' is causing issues when using the XML directly as a string. This is because an apersand is an escape character in XML. You can escape this character with '&amp;' but it is not necessary if you load the XML string directly from the website.

Below is my implementation of parsing this XML document.

// I needed to escape the '&' character in 'AT&T Center' when using a regular string
xmlStr = 
"\[
<sports-event>
	<event-metadata xmlns:fs="java.io.File" date-coverage-type="event" event-key="l.nba.com-e.896939" event-status="pre-event" date-coverage-value="l.nba.com-e.896939" start-date-time="20160114T200000-0500" time-certainty="certain" event-name="Regular Season"site-alignment="home">
		<site>
			<site-metadata site-key="l.nba.com-s.538102" capacity="18581">
				<name full="AT&amp;T Center"/>
				<home-location city="San Antonio" state="Texas" country="USA"/>
			</site-metadata>
			<site-stats/>
		</site>
	</event-metadata>
	<team id="l.nba.com-t.21">
		<team-metadata xmlns:fs="java.io.File" team-key="l.nba.com-t.21" alignment="home">
			<name first="San Antonio" last="Spurs"/>
		</team-metadata>
		<team-stats date-coverage-type="season-regular"/>
	</team>
	<team id="l.nba.com-t.11">
		<team-metadata xmlns:fs="java.io.File" team-key="l.nba.com-t.11" alignment="away">
			<name first="Cleveland" last="Cavaliers"/>
		</team-metadata>
		<team-stats date-coverage-type="season-regular"/>
	</team>
</sports-event>]\";

// loading the string directly from the website works fine
xmlStr = Load Text File( "http://showcase.xmlteam.com/open_rendition.php?id=2129" );

nt = New Table(
	"NBA Sporting events",
	New Column("StartTime", Character),
	New Column("HomeTeamFirst", Character),
	New Column("HomeTeamLast", Character),
	New Column("AwayTeamFirst", Character),
	New Column("AwayTeamLast", Character),
	New Column("City", Character),
	New Column("State", Character),
	New Column("Country", Character),
	New Column("ArenaName", Character)
	
);


iRow = 0;
Parse XML(
	xmlStr,
	On Element(
		"sports-event",
		Start Tag(
			nt << add row(1);
			iRow++;
		)
	),
	On Element(
		"event-metadata",
		Start Tag(
			nt:StartTime[iRow] = XML Attr( "start-date-time" );
		)
	),
	On Element(
		"home-location",
		Start Tag(
			nt:City[iRow] = XML Attr( "city" );
			nt:State[iRow] = XML Attr( "state" );
			nt:Country[iRow] = XML Attr( "country" );
		)
	),
	On Element(
		"site",
		Start Tag(
			inSite = 1;
		),
		End Tag(
			inSite = 0;
		)
	),
	On Element(
		"team",
		Start Tag(
			inTeam = 1;
		),
		End Tag(
			inTeam = 0;
		)
	),
	On Element(
		"team-metadata",
		Start Tag(
			alignment = XML Attr("alignment");
		)
	),
	On Element(
		"name",
		Start Tag(
			If(
				inSite == 1,
					nt:ArenaName[iRow] = XML Attr( "full" ),
				inTeam == 1,
					Match( alignment,
						"home", 
							nt:HomeTeamFirst[iRow] = XML Attr( "first" );
							nt:HomeTeamLast[iRow] = XML Attr( "last" );
						,
						"away",
							nt:AwayTeamFirst[iRow] = XML Attr( "first" );
							nt:AwayTeamLast[iRow] = XML Attr( "last" );
					)
			)
		)
	)
);

Here is the output data table:

Parse XML Example.PNG

Justin
3 REPLIES
Justin_Chilton

Joined:

Aug 27, 2015

Solution

It's possible that the ampersand character in 'AT&T Center' is causing issues when using the XML directly as a string. This is because an apersand is an escape character in XML. You can escape this character with '&amp;' but it is not necessary if you load the XML string directly from the website.

Below is my implementation of parsing this XML document.

// I needed to escape the '&' character in 'AT&T Center' when using a regular string
xmlStr = 
"\[
<sports-event>
	<event-metadata xmlns:fs="java.io.File" date-coverage-type="event" event-key="l.nba.com-e.896939" event-status="pre-event" date-coverage-value="l.nba.com-e.896939" start-date-time="20160114T200000-0500" time-certainty="certain" event-name="Regular Season"site-alignment="home">
		<site>
			<site-metadata site-key="l.nba.com-s.538102" capacity="18581">
				<name full="AT&amp;T Center"/>
				<home-location city="San Antonio" state="Texas" country="USA"/>
			</site-metadata>
			<site-stats/>
		</site>
	</event-metadata>
	<team id="l.nba.com-t.21">
		<team-metadata xmlns:fs="java.io.File" team-key="l.nba.com-t.21" alignment="home">
			<name first="San Antonio" last="Spurs"/>
		</team-metadata>
		<team-stats date-coverage-type="season-regular"/>
	</team>
	<team id="l.nba.com-t.11">
		<team-metadata xmlns:fs="java.io.File" team-key="l.nba.com-t.11" alignment="away">
			<name first="Cleveland" last="Cavaliers"/>
		</team-metadata>
		<team-stats date-coverage-type="season-regular"/>
	</team>
</sports-event>]\";

// loading the string directly from the website works fine
xmlStr = Load Text File( "http://showcase.xmlteam.com/open_rendition.php?id=2129" );

nt = New Table(
	"NBA Sporting events",
	New Column("StartTime", Character),
	New Column("HomeTeamFirst", Character),
	New Column("HomeTeamLast", Character),
	New Column("AwayTeamFirst", Character),
	New Column("AwayTeamLast", Character),
	New Column("City", Character),
	New Column("State", Character),
	New Column("Country", Character),
	New Column("ArenaName", Character)
	
);


iRow = 0;
Parse XML(
	xmlStr,
	On Element(
		"sports-event",
		Start Tag(
			nt << add row(1);
			iRow++;
		)
	),
	On Element(
		"event-metadata",
		Start Tag(
			nt:StartTime[iRow] = XML Attr( "start-date-time" );
		)
	),
	On Element(
		"home-location",
		Start Tag(
			nt:City[iRow] = XML Attr( "city" );
			nt:State[iRow] = XML Attr( "state" );
			nt:Country[iRow] = XML Attr( "country" );
		)
	),
	On Element(
		"site",
		Start Tag(
			inSite = 1;
		),
		End Tag(
			inSite = 0;
		)
	),
	On Element(
		"team",
		Start Tag(
			inTeam = 1;
		),
		End Tag(
			inTeam = 0;
		)
	),
	On Element(
		"team-metadata",
		Start Tag(
			alignment = XML Attr("alignment");
		)
	),
	On Element(
		"name",
		Start Tag(
			If(
				inSite == 1,
					nt:ArenaName[iRow] = XML Attr( "full" ),
				inTeam == 1,
					Match( alignment,
						"home", 
							nt:HomeTeamFirst[iRow] = XML Attr( "first" );
							nt:HomeTeamLast[iRow] = XML Attr( "last" );
						,
						"away",
							nt:AwayTeamFirst[iRow] = XML Attr( "first" );
							nt:AwayTeamLast[iRow] = XML Attr( "last" );
					)
			)
		)
	)
);

Here is the output data table:

Parse XML Example.PNG

Justin
michaelgingras7

Community Trekker

Joined:

Aug 13, 2016

Hi Justin,

 

Thanks for the great help on this. The big problem was the double use of the "name" element by the publisher and figuring out how to parse that. 

The other problem is that I was trying to parse "name" twice within my Parse XML(). That was causing errors too.

 

The most frustrating part of parsing XML is trying to figure out where the error is as you can't step through it.

 

Thanks again.

-Mike

Justin_Chilton

Joined:

Aug 27, 2015


michaelgingras7 wrote:

The big problem was the double use of the "name" element by the publisher and figuring out how to parse that. 


I have found that to be one of the bigger challenges of parsing XML. The key is to use JSL variables to know what the parent element is (as shown with my 'inSite' and 'inTeam' variables).


michaelgingras7 wrote:

The most frustrating part of parsing XML is trying to figure out where the error is as you can't step through it.


I agree. Since everything is done in one step with Parse XML, it can be difficult to visualize what is happening. Sometimes I will use Print or Show statements in my Start/End Tag scripts to see the order of events that take place. 

Justin