spring-batch-rs 0.3.4

A toolkit for building enterprise-grade batch applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
---
title: XML Examples
description: Complete examples for XML file processing with Spring Batch RS
sidebar:
  order: 3
---

import { Tabs, TabItem, Card, CardGrid, Aside } from '@astrojs/starlight/components';

<Aside type="tip">
  View the complete source: [examples/xml_processing.rs](https://github.com/sboussekeyt/spring-batch-rs/blob/main/examples/xml_processing.rs)
</Aside>

This page provides comprehensive examples for working with XML files using Spring Batch RS.

## Setup

Add the XML feature to your `Cargo.toml`:

```toml
[dependencies]
spring-batch-rs = { version = "0.1", features = ["xml"] }
serde = { version = "1.0", features = ["derive"] }
```

---

## Basic XML Reading

### Reading Elements by Tag

```rust
use spring_batch_rs::{
    core::{step::StepBuilder, item::PassThroughProcessor},
    item::xml::XmlItemReaderBuilder,
    item::logger::LoggerWriter,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename = "book")]
struct Book {
    #[serde(rename = "@id")]
    id: String,
    title: String,
    author: String,
    year: i32,
    price: f64,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let reader = XmlItemReaderBuilder::<Book>::new()
        .tag("book")  // Extract <book> elements
        .capacity(1024)
        .from_path("books.xml")?;

    let writer = LoggerWriterBuilder::<Vehicle>::new().build();
    let processor = PassThroughProcessor::<Book>::new();

    let step = StepBuilder::new("read-xml")
        .chunk::<Book, Book>(10)
        .reader(&reader)
        .processor(&processor)
        .writer(&writer)
        .build();

    let mut execution = spring_batch_rs::core::step::StepExecution::new("read-xml");
    step.execute(&mut execution)?;

    Ok(())
}
```

**Input file (`books.xml`):**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
  <book id="1">
    <title>The Rust Programming Language</title>
    <author>Steve Klabnik</author>
    <year>2018</year>
    <price>39.99</price>
  </book>
  <book id="2">
    <title>Programming Rust</title>
    <author>Jim Blandy</author>
    <year>2021</year>
    <price>49.99</price>
  </book>
</books>
```

<Aside type="tip">
  Use `#[serde(rename = "@field")]` for XML attributes and `#[serde(rename = "$value")]` for text content.
</Aside>

---

## XML Attributes

### Reading Attributes and Elements

```rust
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename = "vehicle")]
struct Vehicle {
    #[serde(rename = "@type")]
    vehicle_type: String,
    #[serde(rename = "@id")]
    id: String,
    make: String,
    model: String,
    year: i32,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let reader = XmlItemReaderBuilder::<Vehicle>::new()
        .tag("vehicle")
        .from_path("vehicles.xml")?;

    Ok(())
}
```

**Input:**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<vehicles>
  <vehicle type="car" id="v1">
    <make>Toyota</make>
    <model>Camry</model>
    <year>2023</year>
  </vehicle>
  <vehicle type="truck" id="v2">
    <make>Ford</make>
    <model>F-150</model>
    <year>2024</year>
  </vehicle>
</vehicles>
```

---

## Complex Nested Structures

### Nested Objects and Arrays

```rust
#[derive(Debug, Deserialize, Serialize, Clone)]
struct Displacement {
    #[serde(rename = "@unit")]
    unit: String,
    #[serde(rename = "$value")]
    value: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
struct Engine {
    #[serde(rename = "@cylinders")]
    cylinders: i32,
    #[serde(rename = "type")]
    engine_type: String,
    displacement: Displacement,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
struct Features {
    #[serde(rename = "feature", default)]
    items: Vec<String>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename = "vehicle")]
#[serde(rename_all = "camelCase")]
struct ComplexVehicle {
    #[serde(rename = "@type")]
    vehicle_type: String,
    #[serde(rename = "@id")]
    id: String,
    make: String,
    model: String,
    year: i32,
    engine: Engine,
    features: Features,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let reader = XmlItemReaderBuilder::<ComplexVehicle>::new()
        .tag("vehicle")
        .capacity(2048)
        .from_path("complex_vehicles.xml")?;

    Ok(())
}
```

**Input:**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<vehicles>
  <vehicle type="car" id="1">
    <make>Toyota</make>
    <model>Camry</model>
    <year>2023</year>
    <engine cylinders="4">
      <type>Inline</type>
      <displacement unit="L">2.5</displacement>
    </engine>
    <features>
      <feature>Bluetooth</feature>
      <feature>Backup Camera</feature>
      <feature>Lane Assist</feature>
    </features>
  </vehicle>
</vehicles>
```

---

## Basic XML Writing

### Writing XML Documents

```rust
use spring_batch_rs::item::xml::XmlItemWriterBuilder;

#[derive(Serialize)]
#[serde(rename = "product")]
struct Product {
    #[serde(rename = "@id")]
    id: u32,
    name: String,
    price: f64,
    category: String,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let writer = XmlItemWriterBuilder::new()
        .root_tag("products")
        .item_tag("product")
        .from_path("output.xml")?;

    Ok(())
}
```

**Output:**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<products>
  <product id="1">
    <name>Laptop</name>
    <price>999.99</price>
    <category>Electronics</category>
  </product>
  <product id="2">
    <name>Mouse</name>
    <price>29.99</price>
    <category>Electronics</category>
  </product>
</products>
```

---

## XML to JSON Transformation

Convert XML to JSON format:

```rust
use spring_batch_rs::{
    core::{job::JobBuilder, step::StepBuilder, item::PassThroughProcessor},
    item::{
        xml::XmlItemReaderBuilder,
        json::JsonItemWriterBuilder,
    },
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let reader = XmlItemReaderBuilder::<Vehicle>::new()
        .tag("vehicle")
        .from_path("vehicles.xml")?;

    let writer = JsonItemWriterBuilder::<Vehicle>::new()
        .pretty_formatter(true)
        .from_path("vehicles.json")?;

    let processor = PassThroughProcessor::<Vehicle>::new();

    let step = StepBuilder::new("xml-to-json")
        .chunk::<Vehicle, Vehicle>(50)
        .reader(&reader)
        .processor(&processor)
        .writer(&writer)
        .build();

    let job = JobBuilder::new().start(&step).build();
    job.run()?;

    Ok(())
}
```

---

## CSV to XML Transformation

Convert CSV to XML format:

```rust
use spring_batch_rs::{
    item::csv::CsvItemReaderBuilder,
    core::item::PassThroughProcessor,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename = "employee")]
struct Employee {
    #[serde(rename = "@id")]
    id: u32,
    first_name: String,
    last_name: String,
    department: String,
    salary: f64,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let reader = CsvItemReaderBuilder::<Employee>::new()
        .has_headers(true)
        .from_path("employees.csv")?;

    let writer = XmlItemWriterBuilder::new()
        .root_tag("employees")
        .item_tag("employee")
        .from_path("employees.xml")?;

    let processor = PassThroughProcessor::<Employee>::new();

    let step = StepBuilder::new("csv-to-xml")
        .chunk::<Employee, Employee>(100)
        .reader(&reader)
        .processor(&processor)
        .writer(&writer)
        .build();

    let job = JobBuilder::new().start(&step).build();
    job.run()?;

    Ok(())
}
```

---

## XML Processing with Transformation

### Data Enrichment

```rust
use spring_batch_rs::core::item::{ItemProcessor, ItemProcessorResult};
use std::collections::HashMap;

#[derive(Deserialize, Clone)]
#[serde(rename = "order")]
struct OrderInput {
    #[serde(rename = "@id")]
    order_id: String,
    product_id: u32,
    quantity: u32,
}

#[derive(Serialize)]
#[serde(rename = "enriched_order")]
struct EnrichedOrder {
    #[serde(rename = "@id")]
    order_id: String,
    product_id: u32,
    product_name: String,
    quantity: u32,
    unit_price: f64,
    total: f64,
}

struct OrderEnricher {
    catalog: HashMap<u32, (String, f64)>,
}

impl ItemProcessor<OrderInput, EnrichedOrder> for OrderEnricher {
    fn process(&self, item: &OrderInput) -> ItemProcessorResult<EnrichedOrder> {
        let (product_name, unit_price) = self.catalog
            .get(&item.product_id)
            .cloned()
            .ok_or_else(|| spring_batch_rs::error::BatchError::ItemProcessor(
                format!("Unknown product: {}", item.product_id)
            ))?;

        let total = unit_price * item.quantity as f64;

        Ok(EnrichedOrder {
            order_id: item.order_id.clone(),
            product_id: item.product_id,
            product_name,
            quantity: item.quantity,
            unit_price,
            total,
        })
    }
}
```

---

## Validation and Filtering

### Validate XML Data

```rust
#[derive(Deserialize, Clone)]
#[serde(rename = "record")]
struct RawRecord {
    #[serde(rename = "@id")]
    id: String,
    value: String,
    status: String,
}

#[derive(Serialize)]
#[serde(rename = "validated_record")]
struct ValidatedRecord {
    #[serde(rename = "@id")]
    id: u32,
    value: f64,
    status: String,
}

struct RecordValidator;

impl ItemProcessor<RawRecord, ValidatedRecord> for RecordValidator {
    fn process(&self, item: &RawRecord) -> ItemProcessorResult<ValidatedRecord> {
        // Validate ID
        let id = item.id.parse::<u32>()
            .map_err(|_| spring_batch_rs::error::BatchError::ItemProcessor(
                format!("Invalid ID: {}", item.id)
            ))?;

        // Validate value
        let value = item.value.parse::<f64>()
            .map_err(|_| spring_batch_rs::error::BatchError::ItemProcessor(
                format!("Invalid value: {}", item.value)
            ))?;

        // Validate status
        if !["active", "pending", "completed"].contains(&item.status.as_str()) {
            return Err(spring_batch_rs::error::BatchError::ItemProcessor(
                format!("Invalid status: {}", item.status)
            ));
        }

        Ok(ValidatedRecord {
            id,
            value,
            status: item.status.clone(),
        })
    }
}
```

---

## CDATA and Special Characters

### Handling CDATA Sections

```rust
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename = "article")]
struct Article {
    #[serde(rename = "@id")]
    id: String,
    title: String,
    #[serde(rename = "$value")]
    content: String,  // Can contain CDATA
}
```

**Input:**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<articles>
  <article id="1">
    <title>Sample Article</title>
    <content><![CDATA[
      This content can contain <special> characters & symbols.
      Line breaks are preserved.
    ]]></content>
  </article>
</articles>
```

---

## Real-World Example: RSS Feed Processing

Process RSS-like feed format:

```rust
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename = "item")]
struct FeedItem {
    title: String,
    link: String,
    description: String,
    #[serde(rename = "pubDate")]
    pub_date: String,
    category: String,
}

#[derive(Serialize)]
struct ProcessedFeedItem {
    title: String,
    url: String,
    summary: String,
    published: String,
    tags: Vec<String>,
}

struct FeedProcessor;

impl ItemProcessor<FeedItem, ProcessedFeedItem> for FeedProcessor {
    fn process(&self, item: &FeedItem) -> ItemProcessorResult<ProcessedFeedItem> {
        // Clean HTML from description
        let summary = item.description
            .replace("<p>", "")
            .replace("</p>", "")
            .replace("<br>", " ");

        // Parse categories into tags
        let tags: Vec<String> = item.category
            .split(',')
            .map(|s| s.trim().to_string())
            .collect();

        Ok(ProcessedFeedItem {
            title: item.title.clone(),
            url: item.link.clone(),
            summary,
            published: item.pub_date.clone(),
            tags,
        })
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let reader = XmlItemReaderBuilder::<FeedItem>::new()
        .tag("item")
        .capacity(2048)
        .from_path("feed.xml")?;

    let processor = FeedProcessor;

    let writer = JsonItemWriterBuilder::<Vehicle>::new()
        .pretty_formatter(true)
        .from_path("processed_feed.json")?;

    let step = StepBuilder::new("process-feed")
        .chunk::<FeedItem, ProcessedFeedItem>(50)
        .reader(&reader)
        .processor(&processor)
        .writer(&writer)
        .build();

    let job = JobBuilder::new().start(&step).build();
    job.run()?;

    Ok(())
}
```

---

## Common Patterns

### Text Content with Attributes

```rust
#[derive(Deserialize, Serialize, Clone)]
struct Price {
    #[serde(rename = "@currency")]
    currency: String,
    #[serde(rename = "$value")]
    amount: f64,
}
```

**XML:**
```xml
<price currency="USD">99.99</price>
```

### Optional Elements

```rust
#[derive(Deserialize, Serialize, Clone)]
#[serde(rename = "product")]
struct Product {
    name: String,
    #[serde(default)]
    description: Option<String>,
    price: f64,
}
```

### Default Values

```rust
#[derive(Deserialize, Serialize, Clone)]
struct Config {
    #[serde(default = "default_timeout")]
    timeout: u32,
}

fn default_timeout() -> u32 {
    30
}
```

---

## Performance Tips

<CardGrid>
  <Card title="Buffer Size" icon="rocket">
    Increase `capacity()` for documents with large elements (default: 8192 bytes)
  </Card>
  <Card title="Tag Selection" icon="star">
    Choose specific tag names to extract only needed elements
  </Card>
  <Card title="Memory Usage" icon="warning">
    XML parsing is streaming - memory usage is proportional to element size, not file size
  </Card>
  <Card title="Chunk Size" icon="setting">
    Use moderate chunk sizes (50-100) for XML due to parsing overhead
  </Card>
</CardGrid>

## Troubleshooting

<Aside type="caution">
**Common Issues:**

1. **Attribute not found**: Add `#[serde(rename = "@field")]` for XML attributes
2. **Text content missing**: Use `#[serde(rename = "$value")]` for element text
3. **Parsing errors**: Check XML is well-formed and matches struct definition
4. **Buffer overflow**: Increase `capacity()` for large elements
</Aside>

## Next Steps

- [JSON Examples](/spring-batch-rs/examples/json/) - Process JSON files
- [Database Examples](/spring-batch-rs/examples/database/) - Read/write databases
- [API Reference](/spring-batch-rs/api/item-reader/) - Complete API documentation