XML is commonly used to store and transmit data within and between applications. It is often a requirement where developers need to edit the XML file when it is received or before transmitting. In this article, we will discuss how to edit the XML file data in Java.
Java API to Edit XML Files
GroupDocs.Editor for Java API allows you to edit documents of various file formats. In this article, we will use it to edit XML files. You can use the API along with the external editors for visual editing.
<repository> <id>GroupDocsJavaAPI</id> <name>GroupDocs Java API</name> <url>http://repository.groupdocs.com/repo/</url> </repository> <dependency> <groupId>com.groupdocs</groupId> <artifactId>groupdocs-editor</artifactId> <version>20.11</version> </dependency>
How to Edit XML Files in Java
Let’s jump to the point and modify the XML data by replacing a value with another. The following are the steps to edit or update the XML file in Java.
- Load the XML data file in the Editor class object.
- Prepare the editing options for the XML using the XmlEditOptions class.
- Create the EditableDocument as source content using the edit method and the prepared editing options.
- Use getContent method of the EditableDocument to extract the original content of the XML file.
- Now edit whatever is required in the XML content.
- Now create a new EditableDocument from the updated XML content using fromMarkup method.
- Use the relevant saving options like WordProcessingSaveOptions or TextSaveOptions to save the updated content in different formats.
- Save the updated XML in any format using the save method.
The following code snippet shows how to edit an XML file in Java and update the data to save it in other formats.
Get a Free License
You can get a free temporary license in order to use the API without the evaluation limitations.
To conclude, today we learned to programmatically edit XML file data in Java. Now you can develop your online XML editor app. To further explore the features of GroupDocs.Editor, visit the documentation. For queries, contact us via the forum.