# How to edit XML file This article demonstrates how to edit XML files and XML documents using Java programming language. > This example demonstrates the opening, editing and saving the XML documents, using different options and adjustments. ## Introduction com.groupdocs.editor has supported importing the documents in [XML (eXtensible Markup Language)](https://docs.fileformat.com/web/xml/) format for a long time. However, in [version 23.5](https://docs.groupdocs.com/editor/java/groupdocs-editor-for-java-23-5-release-notes/) the XML processing mechanism was completely redeveloped and drastically enhanced, XML editing public options were also redesigned and significantly expanded, with new sub-option classes and new public types. Current article describes this new XML processing mechanism and is applicable to the GroupDocs.Editor [version 23.5](https://docs.groupdocs.com/editor/java/groupdocs-editor-for-java-23-5-release-notes/) and above. ## Loading XML documents Loading of the XML documents to the `com.groupdocs.`[`Editor`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/) class is usual and the same as for other formats. There are no dedicated load options for the XML format, it is enough to specify the file itself through file path or [ByteArrayInputStream](https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayInputStream.html). If loading through file path, the file extension does not matter, so you may freely load an XML file not only with the `*.xml`, but with any other extension like `*.csproj`, `*.svg` or any other extension - only valid internal structure matters. Also please note that you cannot treat HTML files like XML — only [XHTML](https://en.wikipedia.org/wiki/XHTML) can be treated like valid XML. Code example below shows loading of the same document by two approaches into the two different [`Editor`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/) instances: ```java String inputFilePath = Constants.SAMPLE_XML; final FileInputStream xmlStream = new FileInputStream(inputFilePath) ; Editor editorFromPath = new Editor(inputFilePath); try {//from the path Editor editorFromStream = new Editor(new FileInputStream(inputFilePath)); try /*JAVA: was using*///from the stream { //Here two Editor instances can separately work with one file } finally { (editorFromStream).dispose(); } } finally { (editorFromPath).dispose(); } ``` ## Editing XML documents Like for other format families in GroupDocs.Editor, there is a special [`XmlEditOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/) class for editing the XML documents. As always, it is not mandatory when editing a document, so the [`Editor.edit()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/editor/#edit--) overload without parameter may be used — GroupDocs.Editor will automatically detect the format and apply the default options. The example below shows such a case: XML document is loaded, edited, and then the edited content, represented with a [`EditableDocument`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/editabledocument/) class, may be passed to the WYSIWYG-editor or any other HTML editing software, or simply saved to the disk, as it is shown in the example. ```java String xmlInputPath = Constants.SAMPLE_XML; String outputPath = Constants.getOutputFilePath(Constants.removeExtension(Path.getFileName(xmlInputPath)), "html"); Editor editor = new Editor(xmlInputPath); try /*JAVA: was using*/ { final EditableDocument edited = editor.edit(); try /*JAVA: was using*/ { //Send to WYSIWYG-editor or somewhere else edited.save(outputPath); } finally { (edited).dispose(); } } finally { (editor).dispose(); } ``` [`XmlEditOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/) class has different properties, some of them are grouped into “wrappers” — special classes [`XmlHighlightOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/) and [`XmlFormatOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlformatoptions/), which are described below in detail. The most useful and important properties, however, are directly inside the [`XmlEditOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/) class. #### Encoding [`java.nio.charset.Charset`](https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html) [`getEncoding()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getEncoding--) This property allows to set the encoding, which will be applied while opening an input XML file (keep in mind that any XML is first of all a text file). By default all XML files are UTF8, so the default value of this option is also UTF8. #### FixIncorrectStructure [`boolean`](https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html) [`getFixIncorrectStructure()](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getFixIncorrectStructure--) GroupDocs.Editor can handle without error or exception any XML document: corrupted, truncated, with invalid structure, it can even treat HTML like XML. However, the representation of such a document may degrade in such cases — for example, it is impossible to properly display the nested hierarchical structure, if it is broken. In order to fix this, the [`getFixIncorrectStructure()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getFixIncorrectStructure--) boolean flag is introduced. If enabled, the GroupDocs.Editor scans the XML document and tries to fix its structure. In particular, it escapes some prohibited characters, properly closes unclosed tags, opens unopened tags, fixes overlapping tags, and so on. Because such document scanning and fixing requires additional computational resources and in general most of XML documents are valid, this mechanism is disabled by default: [`getFixIncorrectStructure()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getFixIncorrectStructure--) has a `false` value. So for enabling it, you must set the `true` value manually. #### RecognizeUris [`boolean`](https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html) [`getRecognizeUris()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getRecognizeUris--) This property enables the mechanism of recognizing and preparing the URIs (web address). By default this mechanism is disabled (`false`) and URIs, if they are present in the text nodes or attribute values inside the XML documents, are represented as an ordinary text. When this property is enabled (`true`), the GroupDocs.Editor scans the XML document for any valid URIs, and if found, represents them as external links in the resultant HTML format: by using the A element. GroupDocs.Editor is searching for URIs in: text nodes, CDATA sections, XML comments, attribute values, DocType definitions. #### RecognizeEmails [`boolean`](https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html) [`getRecognizeEmails()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getRecognizeEmails--) This property is very similar to the [`getRecognizeUris()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getRecognizeUris--): it does the same, but at this time for email addresses. By default it is disabled, so if the email address is present in the input XML, the output HTML will contain it as ordinary text. However, if enabled by setting `true`, all valid email addresses will be represented with [mailto](https://en.wikipedia.org/wiki/Mailto) scheme and A element. Unlike the [`getRecognizeUris()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getRecognizeUris--) property, the [`getRecognizeEmails()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getRecognizeEmails--), if enabled, is working only for attribute values. #### TrimTrailingWhitespaces [`boolean`](https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html) [`getTrimTrailingWhitespaces()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getTrimTrailingWhitespaces--) This property enables the truncation of trailing whitespaces in the text nodes — the textual content, located between start and end tags (inner-tag text). By default is disabled (`false`) — trailing whitespaces will be preserved. Line breaks are also treated as whitespaces. May be useful when input XML is formed with line breaks and empty spans for the sake of readability. #### AttributeValuesQuoteType [`QuoteType`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.htmlcss.serialization/quotetype/) [`getAttributeValuesQuoteType()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getAttributeValuesQuoteType--) [`QuoteType`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.htmlcss.serialization/quotetype/) is a new type, introduced in the [version 23.5](https://docs.groupdocs.com/editor/java/groupdocs-editor-for-java-23-5-release-notes/) — it is a struct, that represents two types of quotes, permissible in the XML format in attribute values: a *single quote*, represented by the `U+0027 APOSTROPHE` character, and a *double quote*, represented by the `U+0022 QUOTATION MARK` character. With this option users can redefine the quote type, used in the original XML document, and set the desired quote, which should be present in the resultant HTML. By default the double quotes are used. Next and last two properties, — [`getHighlightOptions()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getHighlightOptions--) and [`getFormatOptions()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getFormatOptions--), — have compound values, described below. What is important, that users cannot create instances of wrapping classes [`XmlHighlightOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/) and [`XmlFormatOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlformatoptions/) and set the values of [`getHighlightOptions()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getHighlightOptions--) and [`getFormatOptions()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getFormatOptions--) (including setting them to null). It is intended that only members of [`XmlHighlightOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/) and [`XmlFormatOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlformatoptions/) have to be changed from the default values. ### HighlightOptions The [`getHighlightOptions()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmleditoptions/#getHighlightOptions--) property has a type of [`XmlHighlightOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/). An already created instance of `XmlHighlightOptions` is already set in the `setHighlightOptions()` property and a reference to it cannot be modified; only members of `XmlHighlightOptions` are allowed to modify. XmlHighlightOptions has 6 sub-properties: 1. [`getXmlTagsFontSettings()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/#getXmlTagsFontSettings--) is responsible for representing the font of XML tags, this include both start and end tags, angle brackets with tag names. By default it is a “`Calibri`” font, `12pt` size. 2. [`getAttributeNamesFontSettings()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/#getAttributeNamesFontSettings--) is responsible for representing the font of attribute names. By default is a “`Calibri`” font, `11pt` size, `red` color. 3. [`getAttributeValuesFontSettings()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/#getAttributeValuesFontSettings--) is responsible for representing the font of attribute values. By default is a “`Calibri`” font, `11pt` size, `blue` color. 4. [`getInnerTextFontSettings()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/#getInnerTextFontSettings--) is responsible for representing the font of text nodes (text inside and between XML elements). By default is a "`Times New Roman`" font, `11pt` size, `black` color. 5. [`getHtmlCommentsFontSettings()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/xmlhighlightoptions/#getHtmlCommentsFontSettings--) is responsible for representing the font of HTML comments (XML comments with a syntax of HTML comments), including a pair of opening `