Skip to content

Commit 7fc5dec

Browse files
committed
update docs
1 parent c241b6a commit 7fc5dec

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

docs/Builder_v1.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,40 @@ let result = parser.parse(XMLdata);
479479

480480
const builder = new XMLBuilder(options);
481481
const output = builder.build(result);
482+
```
483+
484+
---
485+
486+
# Round-tripping XML → JSON → XML (XML declaration)
487+
488+
If your input XML contains a declaration like `<?xml version="1.0"?>`, the fast-xml-parser may include it in the output JSON as a `"?xml"` property (unless disabled).
489+
When converting the JSON back to XML, you may want to omit this node to avoid invalid output (e.g., “XML declaration allowed only at the start of the document”).
490+
491+
492+
*Option A (recommended): ignore the declaration while parsing**
493+
```js
494+
import { XMLParser, XMLBuilder } from "fast-xml-parser";
495+
496+
const parser = new XMLParser({ ignoreDeclaration: true });
497+
const builder = new XMLBuilder();
498+
499+
const obj = parser.parse(xmlInput);
500+
const xmlOutput = builder.build(obj);
501+
```
502+
**Option B: Remove the XML declaration node before building**
503+
504+
If you need to preserve the XML declaration during parsing but want to prevent it from being re-emitted during JSON → XML conversion, you can remove the `"?xml"` node before building:
505+
506+
```js
507+
import { XMLParser, XMLBuilder } from "fast-xml-parser";
508+
509+
const parser = new XMLParser();
510+
const builder = new XMLBuilder();
511+
512+
const obj = parser.parse(xmlInput);
513+
514+
// Remove XML declaration before building
515+
delete obj["?xml"];
516+
517+
const xmlOutput = builder.build(obj);
482518
```

0 commit comments

Comments
 (0)