from static method
- String document, {
- bool parseCharacterEntities = true,
- bool parseComments = false,
- bool trimWhitespace = true,
- bool parseCdataAsText = true,
Parses a XML string for its XML document declaration, DocType declaration, and its root element.
If the document
isn't valid XML and contains multiple
top-level elements, only the first parsed element will be
returned as the root element. If you have to work with such
a document, consider using XmlElement's fromString()
method instead.
If parseCharacterEntities
is true
, text
and attribute values
will be parsed for character entities and replaced with their
corresponding characters.
If parseComments
is true
, commments will be scrubbed
from string
before parsing.
If trimWhitespace
is true
, unnecessary whitespace between nodes
will be removed and all remaining whitespace will be replaced with
a single space. trimWhitespace
must not be null
.
If parseCdataAsText
is true
, all CDATA sections will be
returned as XmlText nodes. parseCdataAsText
must not be null
.
Returns null
if the document is empty.
Implementation
static XmlDocument? from(
String document, {
bool parseCharacterEntities = true,
bool parseComments = false,
bool trimWhitespace = true,
bool parseCdataAsText = true,
}) {
if (!parseComments) document = document.removeComments();
if (trimWhitespace) document = document.trimWhitespace();
document = document.trim();
if (document.isEmpty) return null;
final nodes = XmlNode.parseString(
document,
parseCharacterEntities: parseCharacterEntities,
parseComments: true,
trimWhitespace: false,
parseCdataAsText: parseCdataAsText,
);
if (nodes == null) return null;
return XmlDocument(nodes);
}