aboutsummaryrefslogtreecommitdiff
path: root/mammoth/docx/xmlparser.py
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-12-05 11:06:47 +0000
committerMichael Williamson <mike@zwobble.org>2026-12-05 11:06:47 +0000
commit659c4ffe6ade2d8132873ae3f3f3a22df38fdb37 (patch)
treea5e6dab4c6259ee306a4cac6c089876e52823aa1 /mammoth/docx/xmlparser.py
parent6e62be83305ba864caf8e6bc1afff954dc6f3d91 (diff)
Handle XML documents that are missing a root elementHEADmaster
Diffstat (limited to 'mammoth/docx/xmlparser.py')
-rw-r--r--mammoth/docx/xmlparser.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/mammoth/docx/xmlparser.py b/mammoth/docx/xmlparser.py
index 1ba415d..d78e610 100644
--- a/mammoth/docx/xmlparser.py
+++ b/mammoth/docx/xmlparser.py
@@ -1,4 +1,5 @@
import xml.dom.minidom
+import xml.parsers.expat.errors
import cobble
@@ -74,6 +75,9 @@ XmlElement.node_type = node_types.element
XmlText.node_type = node_types.text
+class NoRootElementError(Exception):
+ pass
+
def parse_xml(fileobj, namespace_mapping=None):
if namespace_mapping is None:
@@ -81,7 +85,13 @@ def parse_xml(fileobj, namespace_mapping=None):
else:
namespace_prefixes = dict((uri, prefix) for prefix, uri in namespace_mapping)
- document = xml.dom.minidom.parse(fileobj)
+ try:
+ document = xml.dom.minidom.parse(fileobj)
+ except xml.parsers.expat.ExpatError as error:
+ if error.code == xml.parsers.expat.errors.codes[xml.parsers.expat.errors.XML_ERROR_NO_ELEMENTS]:
+ raise NoRootElementError()
+ else:
+ raise
def convert_node(node):
if node.nodeType == xml.dom.Node.ELEMENT_NODE: