xpath-to-xml

Convenient utility to build XML models by evaluating XPath expressions

License

License

GroupId

GroupId

com.github.simy4.xpath
ArtifactId

ArtifactId

xpath-to-xml
Last Version

Last Version

2.1.7
Release Date

Release Date

Type

Type

pom
Description

Description

xpath-to-xml
Convenient utility to build XML models by evaluating XPath expressions
Project URL

Project URL

http://github.com/SimY4/xpath-to-xml
Source Code Management

Source Code Management

https://github.com/SimY4/xpath-to-xml

Download xpath-to-xml

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.simy4.xpath/xpath-to-xml/ -->
<dependency>
    <groupId>com.github.simy4.xpath</groupId>
    <artifactId>xpath-to-xml</artifactId>
    <version>2.1.7</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.simy4.xpath/xpath-to-xml/
implementation 'com.github.simy4.xpath:xpath-to-xml:2.1.7'
// https://jarcasting.com/artifacts/com.github.simy4.xpath/xpath-to-xml/
implementation ("com.github.simy4.xpath:xpath-to-xml:2.1.7")
'com.github.simy4.xpath:xpath-to-xml:pom:2.1.7'
<dependency org="com.github.simy4.xpath" name="xpath-to-xml" rev="2.1.7">
  <artifact name="xpath-to-xml" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.github.simy4.xpath', module='xpath-to-xml', version='2.1.7')
)
libraryDependencies += "com.github.simy4.xpath" % "xpath-to-xml" % "2.1.7"
[com.github.simy4.xpath/xpath-to-xml "2.1.7"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

XPath-to-XML

Build Status codecov Codacy Badge License

Maven Central Javadocs

Convenient utility to build XML models by evaluating XPath expressions.

Supported XML models

  • DOM
  • DOM4J
  • JDOM
  • Scala XML
  • XOM

Additionally supported models

  • jakarta.json
  • Gson
  • Jackson

Usage

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));
    }
}

Versions

Version
2.1.7