Convenient utility to build XML models by evaluating XPath expressions.
- DOM
- DOM4J
- JDOM
- Scala XML
- XOM
- jakarta.json
- Gson
- Jackson
Include an artifact with necessary model extension into your project:
<dependency>
<groupId>com.github.simy4.xpath</groupId>
<artifactId>xpath-to-xml-dom</artifactId>
<version>2.2.0</version>
</dependency>
Now having a XML structure i.e.:
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
</breakfast_menu>
and one of supported models:
import java.io.StringReader;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
class Example0 {
private static final String xmlSource = "my-xml-source";
public static Document document(String xml) throws Exception {
var documentBuilderFactory = DocumentBuilderFactory.newInstance();
var documentBuilder = documentBuilderFactory.newDocumentBuilder();
var inputSource = new InputSource(new StringReader(xml));
return documentBuilder.parse(inputSource);
}
}
you can:
- alter existing paths
import com.github.simy4.xpath.XmlBuilder;
class Example1 extends Example0 {
public static void main(String... args) throws Exception {
new XmlBuilder()
.put("/breakfast_menu/food[1]/price", "$7.95")
.build(document(xmlSource));
}
}
- append new elements
import com.github.simy4.xpath.XmlBuilder;
class Example2 extends Example0 {
public static void main(String... args) throws Exception {
new XmlBuilder()
.put("/breakfast_menu/food[name='Homestyle Breakfast'][price='$6.95'][description='Two eggs, bacon or sausage, toast, and our ever-popular hash browns']/calories", "950")
.build(document(xmlSource));
}
}
- remove paths
import com.github.simy4.xpath.XmlBuilder;
class Example3 extends Example0 {
public static void main(String... args) throws Exception {
new XmlBuilder()
.remove("/breakfast_menu/food[name='Belgian Waffles']")
.build(document(xmlSource));
}
}
- combine any of the above actions into a single modification action
import com.github.simy4.xpath.XmlBuilder;
class Example4 extends Example0 {
public static void main(String... args) throws Exception {
new XmlBuilder()
.remove("/breakfast_menu/food[name='Homestyle Breakfast']")
.put("/breakfast_menu/food[name='Homestyle Breakfast'][price='$6.95'][description='Two eggs, bacon or sausage, toast, and our ever-popular hash browns']/calories", "950")
.build(document(xmlSource));
}
}