updated xml modules
This commit is contained in:
parent
45accd613b
commit
03744be67e
2 changed files with 22 additions and 22 deletions
|
|
@ -166,12 +166,12 @@ proc attrValue*(my: XmlParser): string {.inline.} =
|
||||||
assert(my.kind == xmlAttribute)
|
assert(my.kind == xmlAttribute)
|
||||||
return my.b
|
return my.b
|
||||||
|
|
||||||
proc PIName*(my: XmlParser): string {.inline.} =
|
proc piName*(my: XmlParser): string {.inline.} =
|
||||||
## returns the processing instruction name for the event ``xmlPI``
|
## returns the processing instruction name for the event ``xmlPI``
|
||||||
assert(my.kind == xmlPI)
|
assert(my.kind == xmlPI)
|
||||||
return my.a
|
return my.a
|
||||||
|
|
||||||
proc PIRest*(my: XmlParser): string {.inline.} =
|
proc piRest*(my: XmlParser): string {.inline.} =
|
||||||
## returns the rest of the processing instruction for the event ``xmlPI``
|
## returns the rest of the processing instruction for the event ``xmlPI``
|
||||||
assert(my.kind == xmlPI)
|
assert(my.kind == xmlPI)
|
||||||
return my.b
|
return my.b
|
||||||
|
|
@ -641,7 +641,7 @@ when isMainModule:
|
||||||
of xmlCharData: echo(x.charData)
|
of xmlCharData: echo(x.charData)
|
||||||
of xmlWhitespace: echo("|$1|" % x.charData)
|
of xmlWhitespace: echo("|$1|" % x.charData)
|
||||||
of xmlComment: echo("<!-- $1 -->" % x.charData)
|
of xmlComment: echo("<!-- $1 -->" % x.charData)
|
||||||
of xmlPI: echo("<? $1 ## $2 ?>" % [x.PIName, x.PIRest])
|
of xmlPI: echo("<? $1 ## $2 ?>" % [x.piName, x.piRest])
|
||||||
of xmlElementStart: echo("<$1>" % x.elementName)
|
of xmlElementStart: echo("<$1>" % x.elementName)
|
||||||
of xmlElementEnd: echo("</$1>" % x.elementName)
|
of xmlElementEnd: echo("</$1>" % x.elementName)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,13 +15,13 @@ import xmldom, os, streams, parsexml, strutils
|
||||||
|
|
||||||
type
|
type
|
||||||
# Parsing errors
|
# Parsing errors
|
||||||
EMismatchedTag* = object of EInvalidValue ## Raised when a tag is not properly closed
|
EMismatchedTag* = object of ValueError ## Raised when a tag is not properly closed
|
||||||
EParserError* = object of EInvalidValue ## Raised when an unexpected XML Parser event occurs
|
EParserError* = object of ValueError ## Raised when an unexpected XML Parser event occurs
|
||||||
|
|
||||||
# For namespaces
|
# For namespaces
|
||||||
xmlnsAttr = tuple[name, value: string, ownerElement: PElement]
|
XmlnsAttr = tuple[name, value: string, ownerElement: PElement]
|
||||||
|
|
||||||
var nsList: seq[xmlnsAttr] = @[] # Used for storing namespaces
|
var nsList: seq[XmlnsAttr] = @[] # Used for storing namespaces
|
||||||
|
|
||||||
proc getNS(prefix: string): string =
|
proc getNS(prefix: string): string =
|
||||||
var defaultNS: seq[string] = @[]
|
var defaultNS: seq[string] = @[]
|
||||||
|
|
@ -43,13 +43,13 @@ proc getNS(prefix: string): string =
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
proc parseText(x: var TXmlParser, doc: var PDocument): PText =
|
proc parseText(x: var XmlParser, doc: var PDocument): PText =
|
||||||
result = doc.createTextNode(x.charData())
|
result = doc.createTextNode(x.charData())
|
||||||
|
|
||||||
proc parseElement(x: var TXmlParser, doc: var PDocument): PElement =
|
proc parseElement(x: var XmlParser, doc: var PDocument): PElement =
|
||||||
var n = doc.createElement("")
|
var n = doc.createElement("")
|
||||||
|
|
||||||
while True:
|
while true:
|
||||||
case x.kind()
|
case x.kind()
|
||||||
of xmlEof:
|
of xmlEof:
|
||||||
break
|
break
|
||||||
|
|
@ -102,10 +102,10 @@ proc parseElement(x: var TXmlParser, doc: var PDocument): PElement =
|
||||||
of xmlComment:
|
of xmlComment:
|
||||||
n.appendChild(doc.createComment(x.charData()))
|
n.appendChild(doc.createComment(x.charData()))
|
||||||
of xmlPI:
|
of xmlPI:
|
||||||
n.appendChild(doc.createProcessingInstruction(x.PIName(), x.PIRest()))
|
n.appendChild(doc.createProcessingInstruction(x.piName(), x.piRest()))
|
||||||
|
|
||||||
of xmlWhitespace, xmlElementClose, xmlEntity, xmlSpecial:
|
of xmlWhitespace, xmlElementClose, xmlEntity, xmlSpecial:
|
||||||
# Unused 'events'
|
discard " Unused \'events\'"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise newException(EParserError, "Unexpected XML Parser event")
|
raise newException(EParserError, "Unexpected XML Parser event")
|
||||||
|
|
@ -114,30 +114,30 @@ proc parseElement(x: var TXmlParser, doc: var PDocument): PElement =
|
||||||
raise newException(EMismatchedTag,
|
raise newException(EMismatchedTag,
|
||||||
"Mismatched tag at line " & $x.getLine() & " column " & $x.getColumn)
|
"Mismatched tag at line " & $x.getLine() & " column " & $x.getColumn)
|
||||||
|
|
||||||
proc loadXMLStream*(stream: PStream): PDocument =
|
proc loadXMLStream*(stream: Stream): PDocument =
|
||||||
## Loads and parses XML from a stream specified by ``stream``, and returns
|
## Loads and parses XML from a stream specified by ``stream``, and returns
|
||||||
## a ``PDocument``
|
## a ``PDocument``
|
||||||
|
|
||||||
var x: TXmlParser
|
var x: XmlParser
|
||||||
open(x, stream, nil, {reportComments})
|
open(x, stream, nil, {reportComments})
|
||||||
|
|
||||||
var XmlDoc: PDocument
|
var xmlDoc: PDocument
|
||||||
var DOM: PDOMImplementation = getDOM()
|
var dom: PDOMImplementation = getDOM()
|
||||||
|
|
||||||
while True:
|
while true:
|
||||||
x.next()
|
x.next()
|
||||||
case x.kind()
|
case x.kind()
|
||||||
of xmlEof:
|
of xmlEof:
|
||||||
break
|
break
|
||||||
of xmlElementStart, xmlElementOpen:
|
of xmlElementStart, xmlElementOpen:
|
||||||
var el: PElement = parseElement(x, XmlDoc)
|
var el: PElement = parseElement(x, xmlDoc)
|
||||||
XmlDoc = dom.createDocument(el)
|
xmlDoc = dom.createDocument(el)
|
||||||
of xmlWhitespace, xmlElementClose, xmlEntity, xmlSpecial:
|
of xmlWhitespace, xmlElementClose, xmlEntity, xmlSpecial:
|
||||||
# Unused 'events'
|
discard " Unused \'events\'"
|
||||||
else:
|
else:
|
||||||
raise newException(EParserError, "Unexpected XML Parser event")
|
raise newException(EParserError, "Unexpected XML Parser event")
|
||||||
|
|
||||||
return XmlDoc
|
return xmlDoc
|
||||||
|
|
||||||
proc loadXML*(xml: string): PDocument =
|
proc loadXML*(xml: string): PDocument =
|
||||||
## Loads and parses XML from a string specified by ``xml``, and returns
|
## Loads and parses XML from a string specified by ``xml``, and returns
|
||||||
|
|
@ -151,7 +151,7 @@ proc loadXMLFile*(path: string): PDocument =
|
||||||
## a ``PDocument``
|
## a ``PDocument``
|
||||||
|
|
||||||
var s = newFileStream(path, fmRead)
|
var s = newFileStream(path, fmRead)
|
||||||
if s == nil: raise newException(EIO, "Unable to read file " & path)
|
if s == nil: raise newException(IOError, "Unable to read file " & path)
|
||||||
return loadXMLStream(s)
|
return loadXMLStream(s)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue