react xml to json parsing & iterating

12,899

You need to convert XML to JSON. Then you will be able to parse the JSON like you ask to render your React App. You will supply your React Component with the necessary parsed JSON to render.

Try using a library such as xml2js: https://www.npmjs.com/package/xml2js

Share:
12,899

Related videos on Youtube

Sumant Kataria
Author by

Sumant Kataria

Updated on June 04, 2022

Comments

  • Sumant Kataria
    Sumant Kataria almost 2 years

    I am using REACT & I want to iterate my XML to read all attributes. But main problem here is all my Nodes are of name "Key". So i am not able to iterate through XML.

    1 solution i thought is that if i parse XML to JSON so that I can iterate through JSON object & then able to read all attributes. I even tried xml2json(https://www.npmjs.com/package/xml2json). But it is throwing an error on Windows. Can anyone tell it's solution.

    Here is my sample XML:

    <Main>
    <Key name="a" level="1">
        <Key name="b" level="2">
            <Key name="c" level="3">
                <Key name="d" level="4">
                </Key>
            </Key>
        </Key>
        <Key name="m" level="2">
            <Key name="n" level="3">
            </Key>
        </Key>
    </Key>
    

    Thanks in advance.