All pastes #821964 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-12-18 13:01 UTC
rendered paste body
require"lxp"
function table_print (tt, indent, done)
  done = done or {}
  indent = indent or 0
  if type(tt) == "table" then
    for key, value in pairs (tt) do
      io.write(string.rep (" ", indent)) -- indent it
      if type (value) == "table" and not done [value] then
        done [value] = true
        io.write(string.format("[%s] => table\n", tostring (key)));
        io.write(string.rep (" ", indent+4)) -- indent it
        io.write("(\n");
        table_print (value, indent + 7, done)
        io.write(string.rep (" ", indent+4)) -- indent it
        io.write(")\n");
      else
        io.write(string.format("[%s] => %s\n", tostring (key), tostring(value)))
      end
    end
  else
    io.write(tt .. "\n")
  end
end

s = [[<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"><channel><title>Digital Planet</title><link>http://news.bbc.co.uk/1/hi/technology/4849402.stm</link><description>How digital technology affects our lives around the world.</description><itunes:summary>How digital technology affects our lives around the world.</itunes:summary><itunes:author>BBC World Service</itunes:author><itunes:owner><itunes:name>BBC</itunes:name><itunes:email>podcast.support@bbc.co.uk</itunes:email></itunes:owner><language>en</language><ttl>720</ttl><image><url>http://www.bbc.co.uk/radio/podcasts/digitalp/assets/_300x300.jpg</url><title>Digital Planet</title><link>http://news.bbc.co.uk/1/hi/technology/4849402.stm</link></image><itunes:image href="http://www.bbc.co.uk/radio/podcasts/digitalp/assets/_300x300.jpg" /><copyright>(C) BBC 2007</copyright><pubDate>Tue, 11 Dec 2007 13:00:04 +0000</pubDate><itunes:category text="Technology" /><itunes:keywords>digital, technology, news, world, BBC, foreign, global, gadgets, spam , blog, wiki</itunes:keywords><media:keywords>digital, technology, news, world, BBC, foreign, global, gadgets, spam , blog, wiki</media:keywords><itunes:explicit>no</itunes:explicit><media:rating scheme="urn:simple">nonadult</media:rating><item><title>DigitalP: 11 Dec 07</title><description>Getting wireless communications up and running after natural disasters, escaping to the virtual work and using your voice as your password.  Gareth Mitchell and Bill Thompson present Digital Planet - the BBC World Service weekly technology programme.</description><itunes:subtitle>Getting wireless communications up and running after natural disasters, escaping to the virtual work and using your voice as your password. Gareth Mitchell and Bill Thompson present Digital Planet - the BBC World Service weekly technology programme....</itunes:subtitle><itunes:summary>Getting wireless communications up and running after natural disasters, escaping to the virtual work and using your voice as your password.  Gareth Mitchell and Bill Thompson present Digital Planet - the BBC World Service weekly technology programme.</itunes:summary><pubDate>Tue, 11 Dec 2007 13:00:00 +0000</pubDate><itunes:duration>26:41</itunes:duration><enclosure url="http://downloads.bbc.co.uk/podcasts/worldservice/digitalp/digitalp_20071211-1300.mp3" length="12878261" type="audio/mpeg" /><guid isPermaLink="false">http://downloads.bbc.co.uk/podcasts/worldservice/digitalp/digitalp_20071211-1300.mp3</guid><link>http://downloads.bbc.co.uk/podcasts/worldservice/digitalp/digitalp_20071211-1300.mp3</link><media:content url="http://downloads.bbc.co.uk/podcasts/worldservice/digitalp/digitalp_20071211-1300.mp3" fileSize="12878261" type="audio/mpeg" medium="audio" expression="full" duration="1601" /><itunes:author>BBC World Service</itunes:author></item></channel></rss>]]

local encodings = { ["US-ASCII"] = true , ["UTF-8"] = true , ["UTF-16"] = true , ["ISO-8859-1"] = true }
local tbl
function parse ( s , base , encoding )
	encoding = encoding or "US-ASCII"
	tbl = { encoding = encoding }
	local callbacks = {
		_nonstrict = true,
		CharacterData = function ( parses , text ) 
			--Called when the parser recognizes an XML CDATA text.
			if string.find ( text , "%S" ) then --  If non-whitespace characters present
				table.insert( tbl  , text )
			end
		end ,
		Comment = function ( parses , text ) 
			--Called when the parser recognizes an XML comment text.
		end ,
		Default = function ( parses , text ) 
			--Called when the parser has a text  corresponding to any characters in the document which wouldn't otherwise be handled. Using this handler has the side effect of turning off expansion of references to internally defined general entities. Instead these references are passed to the default handler.
		end ,
		DefaultExpand = function ( parses , text ) 
			--Called when the parser has a text  corresponding to any characters in the document which wouldn't otherwise be handled. Using this handler doesn't affect expansion of internal entity references.
		end ,
		StartNamespaceDecl = function ( parses , namespaceName ) 
			--Called when the parser detects an XML namespace declaration with namespaceName. Namespace declarations occur inside start tags, but the StartNamespaceDecl handler is called before the StartElement handler for each namespace declared in that start tag.
		end ,
		EndNamespaceDecl = function ( parses , namespaceName ) 
			--Called when the parser detects the ending of an XML namespace with namespaceName. The handling of the end namespace is done after the handling of the end tag for the element the namespace is associated with.
		end ,
		ExternalEntityRef = function ( parses , subparser , base , systemId , publicId ) 
			--Called when the parser detects an external entity reference.
			--The subparser is a LuaExpat parser created with the same callbacks and Expat context as the parser and should be used to parse the external entity.
			--The base parameter is the base to use for relative system identifiers. It is set by parser:setbase and may be nil.
			--The systemId parameter is the system identifier specified in the entity declaration and is never nil.
			--The publicId parameter is the public id given in the entity declaration and may be nil.
		end ,
		NotStandalone = function ( parses ) 
			--Called when the parser detects that the document is not "standalone". This happens when there is an external subset or a reference to a parameter entity, but the document does not have standalone set to "yes" in an XML declaration.
		end ,
		NotationDecl = function ( parses , notationName , base , systemId , publicId ) 
			--Called when the parser detects XML notation declarations with notationName
			--The base parameter is the base to use for relative system identifiers. It is set by parser:setbase and may be nil.
			--The systemId parameter is the system identifier specified in the entity declaration and is never nil.
			--The publicId parameter is the public id given in the entity declaration and may be nil.
		end ,
		ProcessingInstruction = function ( parses , target , data ) 
			--Called when the parser detects XML processing instructions. The target is the first word in the processing instruction. The data is the rest of the characters in it after skipping all whitespace after the initial word.
		end ,
		StartCDataSection = function ( parses ) 
			--Called when the parser detects the begining of an XML CDATA section.
		end ,
		EndCDataSection = function ( parses ) 
			--Called when the parser detects the end of a CDATA section.
		end ,
		StartElement = function ( parses , elementName , attributes ) 
			--Called when the parser detects the begining of an XML element with elementName.
			--The attributes parameter is a Lua table with all the element attribute names and values. The table contains an entry for every attribute in the element start tag and entries for the default attributes for that element.
			--The attributes are listed by name (including the inherited ones) and by position (inherited attributes are not considered in the position list).
			--As an example if the book element has attributes author, title and an optional format attribute (with "printed" as default value),			
			local t = { parent = tbl , tag = elementName , attributes = attributes }
			
			if attributes["encoding"] then local e = text.upper ( attributes["encoding"] ) if encodings [ e ] then 
				t.encoding = e
				parses:setencoding ( e ) 
			end end
			table_print(tbl)	
			local n = #tbl
			tbl[n+1] = t
			tbl = tbl[n]
			--for k,v in pairs(tbl) do print(k,v) end
		end ,
		EndElement = function ( parses , elementName ) 
			--Called when the parser detects the ending of an XML element with elementName.
			table_print(tbl)
			if tbl.encoding then 
				local a , e = tbl
				while not e do
					a= a.parent
					e = a.encoding
				end
				parses:setencoding ( e ) 
			end
			
			tbl = tbl.parent
		end ,
		UnparsedEntityDecl = function ( parses , text ) 
			--Called when the parser receives declarations of unparsed entities. These are entity declarations that have a notation (NDATA) field.
		end ,
	}
	local parser = lxp.new ( callbacks )
	if base then parser:setbase ( base ) end
	parser:setencoding ( encoding )
	parser:parse ( s )
	parser:close ( )
	return callbacks.tbl
end
--[[
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
	<channel>
		<title>Digital Planet</title>
		<link>http://news.bbc.co.uk/1/hi/technology/4849402.stm</link>
		<description>How digital technology affects our lives around the world.</description>
		<itunes:summary>How digital technology affects our lives around the world.</itunes:summary>
		<itunes:author>BBC World Service</itunes:author>
		<itunes:owner>
			<itunes:name>BBC</itunes:name>
			<itunes:email>podcast.support@bbc.co.uk</itunes:email>
		</itunes:owner>
		<language>en</language>
		<ttl>720</ttl>
		<image>
			<url>http://www.bbc.co.uk/radio/podcasts/digitalp/assets/_300x300.jpg</url>
			<title>Digital Planet</title>
			<link>http://news.bbc.co.uk/1/hi/technology/4849402.stm</link>
		</image>
		<itunes:image href="http://www.bbc.co.uk/radio/podcasts/digitalp/assets/_300x300.jpg" />
		<copyright>(C) BBC 2007</copyright>
		<pubDate>Tue, 11 Dec 2007 13:00:04 +0000</pubDate>
		<itunes:category text="Technology" />
		<itunes:keywords>digital, technology, news, world, BBC, foreign, global, gadgets, spam , blog, wiki</itunes:keywords>
		<media:keywords>digital, technology, news, world, BBC, foreign, global, gadgets, spam , blog, wiki</media:keywords>
		<itunes:explicit>no</itunes:explicit>
		<media:rating scheme="urn:simple">nonadult</media:rating>
		<item>
			<title>DigitalP: 11 Dec 07</title>
			<description>Getting wireless communications up and running after natural disasters, escaping to the virtual work and using your voice as your password.  Gareth Mitchell and Bill Thompson present Digital Planet - the BBC World Service weekly technology programme.</description>
			<itunes:subtitle>Getting wireless communications up and running after natural disasters, escaping to the virtual work and using your voice as your password. Gareth Mitchell and Bill Thompson present Digital Planet - the BBC World Service weekly technology programme....</itunes:subtitle>
			<itunes:summary>Getting wireless communications up and running after natural disasters, escaping to the virtual work and using your voice as your password.  Gareth Mitchell and Bill Thompson present Digital Planet - the BBC World Service weekly technology programme.</itunes:summary>
			<pubDate>Tue, 11 Dec 2007 13:00:00 +0000</pubDate>
			<itunes:duration>26:41</itunes:duration>
			<enclosure url="http://downloads.bbc.co.uk/podcasts/worldservice/digitalp/digitalp_20071211-1300.mp3" length="12878261" type="audio/mpeg" />
			<guid isPermaLink="false">http://downloads.bbc.co.uk/podcasts/worldservice/digitalp/digitalp_20071211-1300.mp3</guid>
			<link>http://downloads.bbc.co.uk/podcasts/worldservice/digitalp/digitalp_20071211-1300.mp3</link>
			<media:content url="http://downloads.bbc.co.uk/podcasts/worldservice/digitalp/digitalp_20071211-1300.mp3" fileSize="12878261" type="audio/mpeg" medium="audio" expression="full" duration="1601" />
			<itunes:author>BBC World Service</itunes:author>
		</item>
	</channel>
</rss>
]]