Related blog post: How to Edit XML files in Java
Last active
November 8, 2021 03:55
-
-
Save GroupDocsGists/593b2f80c8ef10c397a89a5683d9530e to your computer and use it in GitHub Desktop.
Edit XML file and Update Values in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Edit XML file by updating values using Java | |
Editor editor = new Editor("path/XMLData.xml"); | |
// Create XML editing options | |
XmlEditOptions editOptions = new XmlEditOptions(); | |
editOptions.setAttributeValuesQuoteType(QuoteType.DoubleQuote); | |
editOptions.setRecognizeEmails(true); | |
editOptions.setRecognizeUris(true); | |
editOptions.setTrimTrailingWhitespaces(true); | |
// Prepare and Edit the Editable Document | |
EditableDocument beforeEdit = editor.edit(editOptions); | |
// Edit XML | |
String originalTextContent = beforeEdit.getContent(); | |
String updatedTextContent = originalTextContent.replace("John", "Samuel"); | |
List<IHtmlResource> allResources = beforeEdit.getAllResources(); | |
// Create new EditableDocument with updated content | |
EditableDocument afterEdit = EditableDocument.fromMarkup(updatedTextContent, allResources); | |
// Create WordProcessing save options | |
WordProcessingSaveOptions wordSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docx); | |
// Create TXT save options | |
TextSaveOptions txtSaveOptions = new TextSaveOptions(); | |
txtSaveOptions.setEncoding(StandardCharsets.UTF_8); | |
// Save edited XML data in DOCX and TXT format | |
editor.save(afterEdit, "path/updated-xml-data.docx", wordSaveOptions); | |
editor.save(afterEdit, "path/updated-xml-data.txt", txtSaveOptions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment