Using the Pull Parser

This API is experimental, and subject to change.

parser = PullParser.new( "<a>text<b att='val'/>txet</a>" )
while parser.has_next?
  res = parser.next
  puts res[1]['att'] if res.start_tag? and res[0] == 'b'
end

See the PullEvent class for information on the content of the results. The data is identical to the arguments passed for the various events to the StreamListener API.

Notice that:

parser = PullParser.new( "<a>BAD DOCUMENT" )
while parser.has_next?
  res = parser.next
  raise res[1] if res.error?
end

Nat Price gave me some good ideas for the API.

Constants

MISSING_ATTRIBUTE_QUOTES = /^<#{NAME_STR}\s+#{NAME_STR}\s*=\s*[^"']/um

DEFAULT_ENTITIES = { 'gt' => [/>/, '>', '>', />/], 'lt' => [/</, '<', '<', / [/"/, '"', '"', /"/], "apos" => [/'/, "'", "'", /'/] }

EREFERENCE = /&(?!#{NAME};)/

ENTITYDECL = /\s*(?:#{GEDECL})|(?:#{PEDECL})/um

PEREFERENCE = "%#{NAME};"

NDATADECL = "\\s+NDATA\\s+#{NAME}"

EXTERNALID = "(?:(?:(SYSTEM)\\s+#{SYSTEMLITERAL})|(?:(PUBLIC)\\s+#{PUBIDLITERAL}\\s+#{SYSTEMLITERAL}))"

PUBIDLITERAL = %Q{("[#{PUBIDCHAR}']*"|'[#{PUBIDCHAR}]*')}

SYSTEMLITERAL = %Q{((?:"[^"]*")|(?:'[^']*'))}

PUBIDCHAR = "\x20\x0D\x0Aa-zA-Z0-9\\-()+,./:=?;!*@$_%#"

TEXT_PATTERN = /\A([^<]*)/um

SYSTEM = /\A\s*/um

PUBLIC = /\A\s*/um

NOTATIONDECL_START = /\A\s*

ATTLISTDECL_PATTERN = /\A\s*/um

ATTLISTDECL_START = /\A\s*

ATTDEF_RE = /#{ATTDEF}/

ATTDEF = "\\s+#{NAME}\\s+#{ATTTYPE}\\s+#{DEFAULTDECL}"

DEFAULTDECL = "(#REQUIRED|#IMPLIED|(?:(#FIXED\\s+)?#{ATTVALUE}))"

ATTVALUE = "(?:\"((?:[^<&\"]|#{REFERENCE})*)\")|(?:'((?:[^<&']|#{REFERENCE})*)')"

ATTTYPE = "(CDATA|ID|IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS|#{ENUMERATEDTYPE})"

ENUMERATEDTYPE = "(?:(?:#{NOTATIONTYPE})|(?:#{ENUMERATION}))"

NOTATIONTYPE = "NOTATION\\s+\\(\\s*#{NAME}(?:\\s*\\|\\s*#{NAME})*\\s*\\)"

ENUMERATION = "\\(\\s*#{NMTOKEN}(?:\\s*\\|\\s*#{NMTOKEN})*\\s*\\)"

SYSTEMENTITY = /\A\s*(%.*?;)\s*$/um

ELEMENTDECL_PATTERN = /\A\s*(/um

ELEMENTDECL_START = /\A\s*

IDENTITY = /^([!\*\w\-]+)(\s+#{NCNAME_STR})?(\s+["'](.*?)['"])?(\s+['"](.*?)["'])?/u

ENTITY_START = /\A\s*

STANDALONE = /\bstandalone\s*=\s*["'](.*?)['"]/um

ENCODING = /\bencoding\s*=\s*["'](.*?)['"]/um

VERSION = /\bversion\s*=\s*["'](.*?)['"]/um

CLOSE_MATCH = /^\s*<\/(#{NAME_STR})\s*>/um

TAG_MATCH = /^<((?>#{NAME_STR}))\s*((?>\s+#{UNAME_STR}\s*=\s*(["']).*?\5)*)\s*(\/)?>/um

INSTRUCTION_PATTERN = /<\?(.*?)(\s+.*?)?\?>/um

INSTRUCTION_START = /\A<\?/u

GEDECL = ""

PEDECL = ""

ENTITYDEF = "(?:#{ENTITYVALUE}|(?:#{EXTERNALID}(#{NDATADECL})?))"

PEDEF = "(?:#{ENTITYVALUE}|#{EXTERNALID})"

ENTITYVALUE = %Q{((?:"(?:[^%&"]|#{PEREFERENCE}|#{REFERENCE})*")|(?:'([^%&']|#{PEREFERENCE}|#{REFERENCE})*'))}

XMLDECL_PATTERN = /<\?xml\s+(.*?)\?>/um

XMLDECL_START = /\A<\?xml\s/u;

CDATA_PATTERN = //um

CDATA_END = /\A\s*\]\s*>/um

CDATA_START = /\A

COMMENT_PATTERN = //um

COMMENT_START = /\A