0

i'm trying to parse an XML string formed by a sequence of data of type

<id>1233</id><status>on</status><name>name1</name><id>548</id><status>off</status><name>name2</name>....

this string from 10 to 100 object status,id and name and i'd like to return a string like " 1233 is on , 548 is off..." but I don't know how to but isolate the tags. I hope you can help me

2 Answers2

0

You can use fast-xml-parser to parse any xml, this gives you a lot of flexibility.

Once we have parsed to standard objects, it's easy to loop through and display status.

For example:

const xml = `<root>
    <object>
        <id>1233</id>
        <status>on</status>
        <name>name1</name>
    </object>
    <object>
        <id>548</id>
        <status>off</status>
        <name>name2</name>
    </object>
</root>`;

let options = {};
let parsedObjects = parser.parse(xml, options);

console.log("Status:");
parsedObjects.root.object.forEach(obj => console.log(`${obj.id} (${obj.name}) is ${obj.status}`));

console.log("\nParsed objects:", parsedObjects);
<script src="https://cdnjs.cloudflare.com/ajax/libs/fast-xml-parser/3.16.0/parser.js"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
0

you might be interested to try txml. it is a very small xml parser, designed to run everywhere. the code is very clean and it is very fast: https://www.npmjs.com/package/txml

Tobias Nickel
  • 512
  • 5
  • 7