added xmldom modules
This commit is contained in:
parent
44ed48ffa6
commit
d4107728d3
5 changed files with 1136 additions and 2 deletions
13
doc/lib.txt
13
doc/lib.txt
|
|
@ -68,6 +68,9 @@ String handling
|
||||||
is done in O(1) instead of O(n).
|
is done in O(1) instead of O(n).
|
||||||
|
|
||||||
* `unidecode <unidecode.html>`_
|
* `unidecode <unidecode.html>`_
|
||||||
|
This module provides Unicode to ASCII transliterations:
|
||||||
|
It finds the sequence of ASCII characters that is the closest approximation
|
||||||
|
to the Unicode string.
|
||||||
|
|
||||||
|
|
||||||
Generic Operating System Services
|
Generic Operating System Services
|
||||||
|
|
@ -162,6 +165,16 @@ Parsers
|
||||||
scheme for lexers and parsers. This is used by the diverse parsing modules.
|
scheme for lexers and parsers. This is used by the diverse parsing modules.
|
||||||
|
|
||||||
|
|
||||||
|
XML Processing
|
||||||
|
--------------
|
||||||
|
|
||||||
|
* `xmldom <xmldom.html>`_
|
||||||
|
This module implements the XML DOM Level 2.
|
||||||
|
|
||||||
|
* `xmldomparser <xmldomparser.html>`_
|
||||||
|
This module parses a XML Document into a XML DOM Document representation.
|
||||||
|
|
||||||
|
|
||||||
Code generation
|
Code generation
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
|
|
|
||||||
1009
lib/pure/xmldom.nim
Normal file
1009
lib/pure/xmldom.nim
Normal file
File diff suppressed because it is too large
Load diff
110
lib/pure/xmldomparser.nim
Normal file
110
lib/pure/xmldomparser.nim
Normal file
|
|
@ -0,0 +1,110 @@
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Nimrod's Runtime Library
|
||||||
|
# (c) Copyright 2010 Dominik Picheta
|
||||||
|
#
|
||||||
|
# See the file "copying.txt", included in this
|
||||||
|
# distribution, for details about the copyright.
|
||||||
|
#
|
||||||
|
|
||||||
|
import xmldom, os, streams, parsexml, strutils
|
||||||
|
|
||||||
|
## This module parses a XML Document into a XML DOM Document representation.
|
||||||
|
|
||||||
|
#XMLDom's Parser - Turns XML into a Document
|
||||||
|
|
||||||
|
type
|
||||||
|
#Parsing errors
|
||||||
|
EMismatchedTag* = object of E_Base ## Raised when a tag is not properly closed
|
||||||
|
|
||||||
|
template newException(exceptn, message: expr): expr =
|
||||||
|
block: # open a new scope
|
||||||
|
var
|
||||||
|
e: ref exceptn
|
||||||
|
new(e)
|
||||||
|
e.msg = message
|
||||||
|
e
|
||||||
|
|
||||||
|
proc parseText(x: var TXmlParser, doc: var PDocument): PText =
|
||||||
|
result = doc.createTextNode(x.charData())
|
||||||
|
|
||||||
|
proc parseElement(x: var TXmlParser, doc: var PDocument): PElement =
|
||||||
|
var n = doc.createElement("")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
case x.kind()
|
||||||
|
of xmlEof:
|
||||||
|
break
|
||||||
|
of xmlElementStart:
|
||||||
|
if n.tagName() != "":
|
||||||
|
n.appendChild(parseElement(x, doc))
|
||||||
|
else:
|
||||||
|
n = doc.createElement(x.elementName)
|
||||||
|
of xmlElementOpen:
|
||||||
|
if n.tagName() != "":
|
||||||
|
n.appendChild(parseElement(x, doc))
|
||||||
|
else:
|
||||||
|
if x.elementName.contains(':'):
|
||||||
|
#TODO: NamespaceURI
|
||||||
|
n = doc.createElementNS("nil", x.elementName)
|
||||||
|
else:
|
||||||
|
n = doc.createElement(x.elementName)
|
||||||
|
|
||||||
|
of xmlElementEnd:
|
||||||
|
if x.elementName == n.nodeName:
|
||||||
|
return n
|
||||||
|
else: #The wrong element is ended
|
||||||
|
raise newException(EMismatchedTag, "Mismatched tag at line " &
|
||||||
|
$x.getLine() & " column " & $x.getColumn)
|
||||||
|
|
||||||
|
of xmlCharData:
|
||||||
|
n.appendChild(parseText(x, doc))
|
||||||
|
of xmlAttribute:
|
||||||
|
if x.attrKey.contains(':'):
|
||||||
|
#TODO: NamespaceURI
|
||||||
|
n.setAttributeNS("nil", x.attrKey, x.attrValue)
|
||||||
|
else:
|
||||||
|
n.setAttribute(x.attrKey, x.attrValue)
|
||||||
|
of xmlCData:
|
||||||
|
n.appendChild(doc.createCDATASection(x.charData()))
|
||||||
|
of xmlComment:
|
||||||
|
n.appendChild(doc.createComment(x.charData()))
|
||||||
|
of xmlPI:
|
||||||
|
n.appendChild(doc.createProcessingInstruction(x.PIName(), x.PIRest()))
|
||||||
|
else:
|
||||||
|
# echo(x.kind()) # XXX do nothing here!?
|
||||||
|
x.next()
|
||||||
|
|
||||||
|
raise newException(EMismatchedTag,
|
||||||
|
"Mismatched tag at line " & $x.getLine() & " column " & $x.getColumn)
|
||||||
|
|
||||||
|
proc loadXML*(path: string): PDocument =
|
||||||
|
## Loads and parses XML from file specified by ``path``, and returns
|
||||||
|
## a ``PDocument``
|
||||||
|
|
||||||
|
var s = newFileStream(path, fmRead)
|
||||||
|
if s == nil: raise newException(EIO, "Unable to read file " & path)
|
||||||
|
|
||||||
|
var x: TXmlParser
|
||||||
|
open(x, s, path, {reportComments})
|
||||||
|
|
||||||
|
var XmlDoc: PDocument
|
||||||
|
var DOM: PDOMImplementation = getDOM()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
x.next()
|
||||||
|
case x.kind()
|
||||||
|
of xmlEof:
|
||||||
|
break
|
||||||
|
of xmlElementStart, xmlElementOpen:
|
||||||
|
var el: PElement = parseElement(x, XmlDoc)
|
||||||
|
XmlDoc = dom.createDocument(el)
|
||||||
|
else:
|
||||||
|
# echo(x.kind())
|
||||||
|
|
||||||
|
return XmlDoc
|
||||||
|
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
var xml = loadXML(r"C:\Users\Dominik\Desktop\Code\Nimrod\xmldom\test.xml")
|
||||||
|
echo($xml)
|
||||||
|
|
@ -34,6 +34,8 @@ Additions
|
||||||
- Added ``httpclient`` module.
|
- Added ``httpclient`` module.
|
||||||
- Added ``parseutils`` module.
|
- Added ``parseutils`` module.
|
||||||
- Added ``unidecode`` module.
|
- Added ``unidecode`` module.
|
||||||
|
- Added ``xmldom`` module.
|
||||||
|
- Added ``xmldomparser`` module.
|
||||||
- Many wrappers now do not contain redundant name prefixes (like ``GTK_``,
|
- Many wrappers now do not contain redundant name prefixes (like ``GTK_``,
|
||||||
``lua``). The new wrappers are available in ``lib/newwrap``. Change
|
``lua``). The new wrappers are available in ``lib/newwrap``. Change
|
||||||
your configuration file to use these.
|
your configuration file to use these.
|
||||||
|
|
|
||||||
|
|
@ -30,9 +30,9 @@ srcdoc: "pure/parsecfg;pure/parsexml;pure/parsecsv;pure/parsesql"
|
||||||
srcdoc: "pure/streams;pure/terminal;pure/cgi;impure/web;pure/unicode"
|
srcdoc: "pure/streams;pure/terminal;pure/cgi;impure/web;pure/unicode"
|
||||||
srcdoc: "impure/zipfiles;pure/xmlgen;pure/macros;pure/parseutils;pure/browsers"
|
srcdoc: "impure/zipfiles;pure/xmlgen;pure/macros;pure/parseutils;pure/browsers"
|
||||||
srcdoc: "impure/db_postgres;impure/db_mysql;pure/httpserver;pure/httpclient"
|
srcdoc: "impure/db_postgres;impure/db_mysql;pure/httpserver;pure/httpclient"
|
||||||
srcdoc: "pure/ropes"
|
srcdoc: "pure/ropes;pure/unidecode/unidecode;pure/xmldom;pure/xmldomparser"
|
||||||
|
|
||||||
webdoc: "wrappers/libcurl;pure/md5;wrappers/mysql"
|
webdoc: "wrappers/libcurl;pure/md5;wrappers/mysql;wrappers/iup"
|
||||||
webdoc: "wrappers/sqlite3;wrappers/python;wrappers/tcl"
|
webdoc: "wrappers/sqlite3;wrappers/python;wrappers/tcl"
|
||||||
|
|
||||||
webdoc: "posix/posix;wrappers/odbcsql;impure/dialogs"
|
webdoc: "posix/posix;wrappers/odbcsql;impure/dialogs"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue