Merge pull request #7356 from GULPF/parseXml-string
Add string overload for parseXml
This commit is contained in:
commit
58cda83d9e
1 changed files with 15 additions and 10 deletions
|
|
@ -12,9 +12,9 @@
|
||||||
import streams, parsexml, strtabs, xmltree
|
import streams, parsexml, strtabs, xmltree
|
||||||
|
|
||||||
type
|
type
|
||||||
XmlError* = object of ValueError ## exception that is raised
|
XmlError* = object of ValueError ## Exception that is raised
|
||||||
## for invalid XML
|
## for invalid XML.
|
||||||
errors*: seq[string] ## all detected parsing errors
|
errors*: seq[string] ## All detected parsing errors.
|
||||||
|
|
||||||
{.deprecated: [EInvalidXml: XmlError].}
|
{.deprecated: [EInvalidXml: XmlError].}
|
||||||
|
|
||||||
|
|
@ -102,8 +102,8 @@ proc parse(x: var XmlParser, errors: var seq[string]): XmlNode =
|
||||||
|
|
||||||
proc parseXml*(s: Stream, filename: string,
|
proc parseXml*(s: Stream, filename: string,
|
||||||
errors: var seq[string]): XmlNode =
|
errors: var seq[string]): XmlNode =
|
||||||
## parses the XML from stream `s` and returns a ``PXmlNode``. Every
|
## Parses the XML from stream ``s`` and returns a ``XmlNode``. Every
|
||||||
## occurred parsing error is added to the `errors` sequence.
|
## occurred parsing error is added to the ``errors`` sequence.
|
||||||
var x: XmlParser
|
var x: XmlParser
|
||||||
open(x, s, filename, {reportComments})
|
open(x, s, filename, {reportComments})
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -121,15 +121,20 @@ proc parseXml*(s: Stream, filename: string,
|
||||||
close(x)
|
close(x)
|
||||||
|
|
||||||
proc parseXml*(s: Stream): XmlNode =
|
proc parseXml*(s: Stream): XmlNode =
|
||||||
## parses the XTML from stream `s` and returns a ``PXmlNode``. All parsing
|
## Parses the XML from stream ``s`` and returns a ``XmlNode``. All parsing
|
||||||
## errors are turned into an ``EInvalidXML`` exception.
|
## errors are turned into an ``XmlError`` exception.
|
||||||
var errors: seq[string] = @[]
|
var errors: seq[string] = @[]
|
||||||
result = parseXml(s, "unknown_html_doc", errors)
|
result = parseXml(s, "unknown_xml_doc", errors)
|
||||||
if errors.len > 0: raiseInvalidXml(errors)
|
if errors.len > 0: raiseInvalidXml(errors)
|
||||||
|
|
||||||
|
proc parseXml*(str: string): XmlNode =
|
||||||
|
## Parses the XML from string ``str`` and returns a ``XmlNode``. All parsing
|
||||||
|
## errors are turned into an ``XmlError`` exception.
|
||||||
|
parseXml(newStringStream(str))
|
||||||
|
|
||||||
proc loadXml*(path: string, errors: var seq[string]): XmlNode =
|
proc loadXml*(path: string, errors: var seq[string]): XmlNode =
|
||||||
## Loads and parses XML from file specified by ``path``, and returns
|
## Loads and parses XML from file specified by ``path``, and returns
|
||||||
## a ``PXmlNode``. Every occurred parsing error is added to the `errors`
|
## a ``XmlNode``. Every occurred parsing error is added to the ``errors``
|
||||||
## sequence.
|
## sequence.
|
||||||
var s = newFileStream(path, fmRead)
|
var s = newFileStream(path, fmRead)
|
||||||
if s == nil: raise newException(IOError, "Unable to read file: " & path)
|
if s == nil: raise newException(IOError, "Unable to read file: " & path)
|
||||||
|
|
@ -137,7 +142,7 @@ proc loadXml*(path: string, errors: var seq[string]): XmlNode =
|
||||||
|
|
||||||
proc loadXml*(path: string): XmlNode =
|
proc loadXml*(path: string): XmlNode =
|
||||||
## Loads and parses XML from file specified by ``path``, and returns
|
## Loads and parses XML from file specified by ``path``, and returns
|
||||||
## a ``PXmlNode``. All parsing errors are turned into an ``EInvalidXML``
|
## a ``XmlNode``. All parsing errors are turned into an ``XmlError``
|
||||||
## exception.
|
## exception.
|
||||||
var errors: seq[string] = @[]
|
var errors: seq[string] = @[]
|
||||||
result = loadXml(path, errors)
|
result = loadXml(path, errors)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue