xpath-to-json-javax

Convenient utility to build XML models by evaluating XPath expressions

Лицензия

Лицензия

Категории

Категории

JSON Данные
Группа

Группа

com.github.simy4.xpath
Идентификатор

Идентификатор

xpath-to-json-javax
Последняя версия

Последняя версия

2.1.9
Дата

Дата

Тип

Тип

module
Описание

Описание

xpath-to-json-javax
Convenient utility to build XML models by evaluating XPath expressions
Ссылка на сайт

Ссылка на сайт

http://github.com/SimY4/xpath-to-xml
Система контроля версий

Система контроля версий

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

Скачать xpath-to-json-javax

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
com.github.simy4.xpath : xpath-to-xml-core jar 2.1.9

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
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.1
2.1.0
2.0.0