Software Development

Modify XML Element Content in Java using XSLT

This post demonstrates how to modify some parts of the XML content in Java using XSLT. The example herein is rather straight-forward but provides basic working codes before moving to more advanced stuff.

[wp_ad_camp_1]

Requirements

Stuff used.

  • Java 8 (JDK)
  • IntelliJ IDEA – optional
  • Spring Boot – optional
  • Windows 10 – optional
  • XSLT 1.0

Purpose

Modify some element text in input.xml using Java based on rules and expressions declared in translation.xsl. The modified XML content is written to output.xml.

We are to prefix the heading value with CONFIDENTIAL: and append Please do not print! to the body value.

[wp_ad_camp_2]

Sample XML and XSL Files

These are the XML files to be used here – input.xml and translation.xsl

Translation.xsl contains rules and expressions on how to go about replacing stuff in the input.xml and produce the output.xml file with some Java codes.

[wp_ad_camp_3]

Target Output XML

The output.xml will have this content:

Java Codes

[wp_ad_camp_4]

[wp_ad_camp_5]

Download the Codes

https://github.com/Turreta/Replace-XML-Contents-in-Java-using-XSLT

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like