XML Splitter
El XML Splitter proporcionado por IPF se implementa utilizando el akka-stream-alpakka-xml biblioteca. Toma una jerarquía de componentes, 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 adicional 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 Dependencia
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>
Configuración
El XML El divisor puede ser configurado con las opciones a continuación para controlar aspectos de los componentes correspondientes en los que será desglosado.
| Por defecto, todos estos están configurados en falso. |
ipf.debulker {
# non-xsi prefixed namespace(s);
# true => <Book xmlns="namespace" xmlns:xsi="schema-instance" xlmns:bk="namespace2">
# false => <Book xmlns="namespace" xmlns:xsi="schema-instance">
should-append-namespaces = false
# element prefixes;
# true => <bk:title>Good Book</bk:title>
# false => <title>Good Book</title>
should-append-prefixes = false
}
Ejemplo de Uso
Imagine que queremos procesar grandes potencialmente XML file s que contienen datos sobre libros en una biblioteca y divídalo en fragmentos más pequeños, para que puedan ser utilizados 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.
example.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<chapter><name>Código Limpio</name><startPage>1</startPage></chapter>)
DebulkComponent(bulkId=null, id=4bd76b08-6dcb-4f09-882e-f20233ed375d,parentId<chapter><name>Nombres Significativos</name><content>Los nombres significativos son fundamentales en la programación. Un nombre debe reflejar la función o el propósito del elemento que representa. Esto facilita la comprensión del código y mejora su mantenibilidad.</content></chapter>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<chapter><name>Introducción</name><content>Este capítulo proporciona una visión general del tema tratado en este documento. Se abordarán los conceptos clave y se establecerán las bases para el desarrollo posterior.</content></chapter>startPage>1</startPage></chapter>)
DebulkComponent(bulkId=null, id=c28d4e18-0610-41b0-bb0a-5f85723788af,parentId<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=library, index=0, content=<library><name>Biblioteca de Alejandría</name></library>)