XML Splitter

El XML Splitter proporcionado por IPF se implementa utilizando el akka-stream-alpakka-xml biblioteca. Toma un component hierarchy, definiendo la estructura de XML elementos a separar.

Vale la pena señalar que el resultado generado XML los datos del elemento serán compactados. Es decir, que cualquier espacio en blanco extráneo en el documento original será eliminado de los componentes que se publiquen. Las versiones futuras pueden incluir configuraciones para preservar el formato de origen.

Maven Dependency

Para utilizar el XML splitter, se debe proporcionar la siguiente dependencia, con una versión que coincida con ipf-debulker-core para garantizar la compatibilidad.

<dependency>
    <groupId>com.iconsolutions.ipf.debulk</groupId>
    <artifactId>ipf-debulker-xml-splitter</artifactId>
    <version>${ipf-debulker-core.version}</version>
</dependency>

Ejemplo de Uso

Imagine que queremos procesar grandes cantidades de XML file s que contienen datos sobre libros en una biblioteca y divídalo en partes más pequeñas, para que puedan ser utilizadas por algún sistema posterior.

El archivo de ejemplo es pequeño para fines de demostración, pero podría contener un gran número de elementos de libro.

ejemplo.xml
<library>
    <name>Library of Alexandria</name>
    <book>
        <author>Martin, Robert</author>
        <title>Clean Code</title>
        <chapter>
            <name>Clean Code</name>
            <startPage>1</startPage>
        </chapter>
        <chapter>
            <name>Meaningful Names</name>
            <startPage>17</startPage>
        </chapter>
    </book>
    <book>
        <author>Bloch, Joshua</author>
        <title>Effective Java</title>
        <chapter>
            <name>Introduction</name>
            <startPage>1</startPage>
        </chapter>
        <chapter>
            <name>Creating and Destroying Objects</name>
            <startPage>5</startPage>
        </chapter>
    </book>
</library>

Given our example XML data, we might decide to split it using the following hierarchy.

library
└── book
    └── chapter

Note: path to the child component should be relative from parent in the hierarchy, in the example above we expect book element to be direct child of library element. If that is not the case, we need to specify relative path to it delimited with ..

With these prerequisites out the way; let’s write the program. Bear in mind, this example makes use of Project Reactor to convert the Java 9 Flow. Publisher to a Flux to make subscribing to the data a bit simpler, but this could be replaced with another reactive library that is compatible with the Java 9 reactive libraries, e.g. RxJava.

ActorSystem system = ActorSystem.create();
Splitter splitter = new AkkaXmlSplitter(system);

InputStream stream = getClass().getClassLoader().getResourceAsStream("example.xml");

ComponentHierarchy root = ComponentHierarchy.root("library");
ComponentHierarchy book = root.addChild("book");
book.addChild("chapter");

Flow.Publisher<DebulkComponent> publisher = splitter.split(stream, root);
Flux<DebulkComponent> flux = JdkFlowAdapter.flowPublisherToFlux(publisher);
List<DebulkComponent> components = flux.collectList().block();

components.forEach(System.out::println);

Running this code should print a series of extracted components to the console.

output
DebulkComponent(bulkId=null, id=2318c220-515d-4904-a71f-daf1bc2f7b1a, parentId=f625f075-a1d3-4865-b0f3-c83aa1f485c6, marker=library.book.chapter, index=2, content=<chapter><name>Código Limpio</name><startPage>1</startPage></chapter>)
DebulkComponent(bulkId=null, id=4bd76b08-6dcb-4f09-882e-f20233ed375d, parentId=f625f075-a1d3-4865-b0f3-c83aa1f485c6, marker<chapter><name>Nombres Significativos</name><startPage>17</startPage></chapter>
DebulkComponent(bulkId=null, id=f625f075-a1d3-4865-b0f3-c83aa1f485c6, parentId=65fdf7ee-9646-4ba2-a654-78fab7d4be43, marker=library.book, index=1, content=<book><author>Martín, Roberto</author><title>Código Limpio</title></book>)
DebulkComponent(bulkId=null, id=9fbf87f5-8873-4512-8c53-29488fd0e0e0, parentId=b763d8e5-880f-43f3-90f2-430eeb33218d,marker<chapter><name>Introducción</name><startPage>1</startPage></chapter>
DebulkComponent(bulkId=null, id=c28d4e18-0610-41b0-bb0a-5f85723788af, parentId=b763d8e5-880f-43f3-90f2-430eeb33218d,marker<chapter><name>Creación y Destrucción de Objetos</name><startPage>5</startPage></chapter>
DebulkComponent(bulkId=null, id=b763d8e5-880f-43f3-90f2-430eeb33218d, parentId=65fdf7ee-9646-4ba2-a654-78fab7d4be43, marker=library.book, index=4, content=<book><author>Bloch, Joshua</author><title>Efectivo Java</title></book>)
DebulkComponent(bulkId=null, id=65fdf7ee-9646-4ba2-a654-78fab7d4be43, parentId=, marker=biblioteca, índice=0, contenido=<biblioteca><nombre>Biblioteca de Alejandría</nombre></biblioteca>)