xpath-to-xml-scala_2.11

Convenient utility to build XML models by evaluating XPath expressions

License

License

Categories

Categories

Scala Languages
GroupId

GroupId

com.github.simy4.xpath
ArtifactId

ArtifactId

xpath-to-xml-scala_2.11
Last Version

Last Version

2.2.3
Release Date

Release Date

Type

Type

module
Description

Description

xpath-to-xml-scala_2.11
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-scala_2.11

Dependencies

compile (1)

Group / Artifact Type Version
com.github.simy4.xpath : xpath-to-xml-core jar 2.2.3

runtime (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
org.scala-lang : scala-reflect jar 2.11.12
org.scala-lang.modules : scala-xml_2.11 jar 1.3.0

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.2.3
2.2.2
2.2.1
2.2.0
2.1.9
2.1.8
2.1.7
2.1.6
2.1.5
2.1.4
2.1.3
2.1.2
2.1.0
2.0.0