fast-xml-parser icon indicating copy to clipboard operation
fast-xml-parser copied to clipboard

XML prolog is included as "?xml" property in json output

Open ulrichstark opened this issue 1 month ago • 3 comments

  • [x] Are you running the latest version?
  • [x] Have you included sample input, output, error, and expected output?
  • [x] Have you checked if you are using correct configuration?
  • [x] Did you try online tool?
  • [x] Have you checked the docs for helpful APIs and examples?

Description

  1. Go to https://naturalintelligence.github.io/fast-xml-parser
  2. Click "Parse to JSON"
  3. Click "Parse to XML"
  4. Click "Parse to JSON"
  5. See error "Error: XML declaration allowed only at the start of the document.:2:6" in JSON panel

Input

(any XML code containing the prolog)

Code

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

Output

{
    "?xml": "",
    "note": {
        "to": "Tove",
        "from": "Jani",
        "heading": "Reminder",
        "body": "Don't forget me this weekend!"
    }
}

expected data

{
    "note": {
        "to": "Tove",
        "from": "Jani",
        "heading": "Reminder",
        "body": "Don't forget me this weekend!"
    }
}

Would you like to work on this issue?

  • [X] Yes
  • [ ] No

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

ulrichstark avatar Nov 26 '25 15:11 ulrichstark

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

github-actions[bot] avatar Nov 26 '25 15:11 github-actions[bot]

This is the issue in demo as <?xml version="1.0"?> is hard coded. no issue in the parser or builder

amitguptagwl avatar Dec 12 '25 04:12 amitguptagwl

So you want both xml prologs in /index.html removed right?

ulrichstark avatar Dec 12 '25 07:12 ulrichstark

no issue in the parser or builder

Are you sure about this? The following test fails if the xml prolog is present. Removing the xml prolog make the test pass.

describe("XMLParser with prolog", function () {
    const xmlData = `<?xml version="1.0" encoding="UTF-8"?>
    <note>
        <to>Tove</to>
    </note>`;

    const expected = {
        "note": {
            "to": "Tove",
        }
    };

    it("should parse XML prolog correctly", function () {
        const parser = new XMLParser();
        const result = parser.parse(xmlData);
        expect(result).toEqual(expected);
    });
});

ulrichstark avatar Dec 18 '25 06:12 ulrichstark