[Tutorial] How to fix SAXParse error in LibreOff .docx files
Posted: Fri Jul 28, 2017 5:52 pm
These problems seem only to arise in .docx files saved by LibreOffice.
Try one of the following four self-help methods to fix LibreOffice .docx files with SAX parse errors - you only need to use one of them and you chance of a successful recovery is very high.
0. Always work on a copy of the file because, if things go wrong, you will still have the original.
1. The free Microsoft Word Viewer seems to be able to open these files ... EDIT: It may only show the content before the error, and everything after the error is truncated
... so download it from How to obtain the latest Microsoft Word Viewer. Open the file and copy the data into a Writer document.
Why can Microsoft Word Viewer open them? Presumably because Microsoft Word Viewer has better, more robust error handling code than LO, and can cope with the repeated attribute without throwing an error.
It is also very useful always to have the Viewer available in case you have a document with MS Word Textboxes because AOO does not display Textboxes (though LO does).
2. AOO seems to be able to open these files ... EDIT: It may only show the content before the error, and everything after the error is truncated
... so download Apache OpenOffice from http://www.openoffice.org/download/index.html. Create a new user on your PC and install AOO for that user only. AOO and LO seem to interact in that LO grabs some of the AOO properties and this will completely isolate AOO from LO. Open the .docx file with AOO. Save it as a .odt file. Uninstall AOO and delete the added user. Note that this will delete any MS Word Textboxes and their contents because AOO does not support MS Word Textboxes (presumably because they are not part of the OOXML standard).
Why can AOO open them? Presumably because AOO has better, more robust error handling code than LO, and AOO can cope with the repeated attribute without throwing an error.
3. Remove the repeated definitions from document.xml
This requires you to unzip the .docx file, extract the \word\document.xml file, and remove all the occurrences of the repeated attribute specified in the error message you get when you open the .docx file. Note that there may be more than one attribute repeated in the file so you may have to do this for the other repeated attribute(s). Repeated attributes reported here include w:themeShade, w:themeColor and w:cstheme. Some files uploaded to the forum have had many (30+?) repeats.
1 Unzip the .docx file and extract \word\document.xml.
A .docx file is actually a ZIP file so just unZIP it; or rename fred.docx to fred.zip, and double click it.
2 Open document.xml in Notepad++ and search for the repeated attribute (eg w:themeColor)
Delete the second instance of w:themeColor="accent1" each time it occurs, leaving the trailing /> as below:
Before correcting ...
After correcting ...
3 Put document.xml back into the .docx file in the \word folder.
If you renamed fred.docx to fred.zip then drag document.xml back into it and rename fred.zip back to fred.docx.
The .docx file should now open properly.
Note that it is easier to find the repeated occurrences if you "pretty print" the XML using the XML Tools plugin for Notepad++. BUT - if you use Pretty print, be sure to Linearise the XML before saving it (it is an XML Tools option) or lots of tabs and newlines will be saved in the file which then appear in the repaired document.
4. Extract \word\document.xml from the .docx file and strip off all the XML tags to leave just the text
Windows:
Rename the file from fred.docx to fred.ZIP.
Double click fred.ZIP.
Navigate to the \word folder.
Drag document.XML onto the desktop.
- Install Notepad++ and the XML Tools plug-in. Open document.xml with Notepad ++. Go Plugins > XML Tools > Pretty print XML with line breaks. Delete the XML tags leaving just the text.
- Alternatively, Google pretty print and upload document.xml to a pretty print web site which will format it. Delete the XML tags.
Linux:
Rename the file from fred.docx to fred.ZIP.
Unzip fred.ZIP - you may need to install a ZIP utility on Linux.
Navigate to the \word folder.
Extract document.xml.
- Install an XML editor. Open document.xml with the XML editor and format it "pretty print". Delete the XML tags leaving just the text.
- Alternatively, Google pretty print and upload document.xml to a pretty print web site which will format it. Delete the XML tags.
5. See other examples in this Tutorial
See [Tutorial] Format error discovered in sub-document for other examples of SAXParse corruptions.
If everything else fails, your only hope then is to see [Tutorial] How to find and un-delete Writer temporary files for
a) use Previous Versions (W7 and later) to recover previous versions of the file (is there something similar on MacOS and Linux?);
b) recover your file as it was when you last opened or saved it; or as it was when it was last saved with AutoRecovery;
c) find previous versions of the file in the folder it is located in, but which have since been deleted;
d) find any temporary files AOO wrote while you were editing the file but which have not yet been deleted;
e) un-delete the temporary files AOO wrote while you were editing the file, and then deleted. d) and e) will recover your file as it was when you last opened or you last saved it.
Edit: See [Tutorial] Differences between Writer and MS Word files for why you should always work in, and save files, as .odt; and never save a file as a .doc, .docx or .rtf. Only create a .doc, .docx or .rtf as a copy if you have to, but always keep the .odt as the master. |
0. Always work on a copy of the file because, if things go wrong, you will still have the original.
Edit: When you get the SAXParse error message it tells you the name of the repeated attribute - note it as you will need to search for it. The error message goes on to say: Do you want to continue to open the file? If you say YES, everything up to the error is displayed, and everything after the error is missing. If, despite the warning above, you are working on the original file DO NOT NOW SAVE THE FILE (unless you use a different name) BECAUSE YOU WILL DELETE ALL THE MISSING DATA FROM THE .DOCX and you will never then be able to recover the missing data as you will have deleted it from the file. |
... so download it from How to obtain the latest Microsoft Word Viewer. Open the file and copy the data into a Writer document.
Why can Microsoft Word Viewer open them? Presumably because Microsoft Word Viewer has better, more robust error handling code than LO, and can cope with the repeated attribute without throwing an error.
It is also very useful always to have the Viewer available in case you have a document with MS Word Textboxes because AOO does not display Textboxes (though LO does).
2. AOO seems to be able to open these files ... EDIT: It may only show the content before the error, and everything after the error is truncated
... so download Apache OpenOffice from http://www.openoffice.org/download/index.html. Create a new user on your PC and install AOO for that user only. AOO and LO seem to interact in that LO grabs some of the AOO properties and this will completely isolate AOO from LO. Open the .docx file with AOO. Save it as a .odt file. Uninstall AOO and delete the added user. Note that this will delete any MS Word Textboxes and their contents because AOO does not support MS Word Textboxes (presumably because they are not part of the OOXML standard).
Why can AOO open them? Presumably because AOO has better, more robust error handling code than LO, and AOO can cope with the repeated attribute without throwing an error.
3. Remove the repeated definitions from document.xml
This requires you to unzip the .docx file, extract the \word\document.xml file, and remove all the occurrences of the repeated attribute specified in the error message you get when you open the .docx file. Note that there may be more than one attribute repeated in the file so you may have to do this for the other repeated attribute(s). Repeated attributes reported here include w:themeShade, w:themeColor and w:cstheme. Some files uploaded to the forum have had many (30+?) repeats.
1 Unzip the .docx file and extract \word\document.xml.
A .docx file is actually a ZIP file so just unZIP it; or rename fred.docx to fred.zip, and double click it.
2 Open document.xml in Notepad++ and search for the repeated attribute (eg w:themeColor)
Delete the second instance of w:themeColor="accent1" each time it occurs, leaving the trailing /> as below:
Before correcting ...
Code: Select all
<w:rPr>
<w:sz w:val="20"/>
<w:szCs w:val="20"/>
<w:highlight w:val="yellow"/>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:color w:val="5B9BD5" w:themeColor="accent1" w:themeColor="accent1"/>
</w:rPr>
Code: Select all
<w:rPr>
<w:sz w:val="20"/>
<w:szCs w:val="20"/>
<w:highlight w:val="yellow"/>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:color w:val="5B9BD5" w:themeColor="accent1"/>
</w:rPr>
If you renamed fred.docx to fred.zip then drag document.xml back into it and rename fred.zip back to fred.docx.
The .docx file should now open properly.
Note that it is easier to find the repeated occurrences if you "pretty print" the XML using the XML Tools plugin for Notepad++. BUT - if you use Pretty print, be sure to Linearise the XML before saving it (it is an XML Tools option) or lots of tabs and newlines will be saved in the file which then appear in the repaired document.
Edit: Files have recently been analysed where there are additional different errors in the XML. These errors were found and corrected by using the XML Syntax Checker to check the XML. See here |
4. Extract \word\document.xml from the .docx file and strip off all the XML tags to leave just the text
Windows:
Rename the file from fred.docx to fred.ZIP.
Double click fred.ZIP.
Navigate to the \word folder.
Drag document.XML onto the desktop.
- Install Notepad++ and the XML Tools plug-in. Open document.xml with Notepad ++. Go Plugins > XML Tools > Pretty print XML with line breaks. Delete the XML tags leaving just the text.
- Alternatively, Google pretty print and upload document.xml to a pretty print web site which will format it. Delete the XML tags.
Linux:
Rename the file from fred.docx to fred.ZIP.
Unzip fred.ZIP - you may need to install a ZIP utility on Linux.
Navigate to the \word folder.
Extract document.xml.
- Install an XML editor. Open document.xml with the XML editor and format it "pretty print". Delete the XML tags leaving just the text.
- Alternatively, Google pretty print and upload document.xml to a pretty print web site which will format it. Delete the XML tags.
Edit: The easiest way to delete all the XML tags is with a Find and Replace, where you use a Regular Expression to find all the tags. (Note: A regular expression search will work in LO itself as long as you do not break the character limit for a paragraph [64k in AOO, more in LO??].) It works fine in NotePad++. 1. Open document.xml. 2. Go Search > Replace ..., with search argument <[^>]+> and replace argument blank (or space). 3. Tick Regular Expressions. 4. Click Replace All. All XML tags are deleted and you are left with just the text. You will need to re-format it and recreate tables and footnotes etc. If you pretty printed before searching and you do not Linearise the XML before you save the file, you will be left with many tabs which you will need to delete manually. |
See [Tutorial] Format error discovered in sub-document for other examples of SAXParse corruptions.
If everything else fails, your only hope then is to see [Tutorial] How to find and un-delete Writer temporary files for
a) use Previous Versions (W7 and later) to recover previous versions of the file (is there something similar on MacOS and Linux?);
b) recover your file as it was when you last opened or saved it; or as it was when it was last saved with AutoRecovery;
c) find previous versions of the file in the folder it is located in, but which have since been deleted;
d) find any temporary files AOO wrote while you were editing the file but which have not yet been deleted;
e) un-delete the temporary files AOO wrote while you were editing the file, and then deleted. d) and e) will recover your file as it was when you last opened or you last saved it.