有没有可能从 xml 读取每个节点

is there any possible to read each node from xml

我正在使用 Angular 7.

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <Employees>
    <Employee>
    <Name>nameOne</Name>
    <Id>IdOne</Id>
    </Employee>
    <Employee>
    <Employee-Name>empName</Employee-Name>
    </Employee>
    <Employee>
    <Employee-Id>IdEmp</Employee-Id>
    </Employee>
    </Employees>
</note>
const parser = new xml2js.Parser({ strict: false, trim: true });
parser.parseString(uploadedXMLData, (err, result) => {
    const obj: any[] = result;
    console.log(result);
    Object.entries(obj).forEach((data, index) => {
        console.log('Data::: ', data, index);
        data[0]['NOTE'].forEach((empData, indexNumber) => {
            console.log('Emp Data::: ', empData, indexNumber);
        });
    });
});

要到达每个节点,尽管XML的结构你需要使用递归,就像这里:

function printNode(xml, key?) {
    if (xml == null) {
        console.log(`Node is empty`);
        return;
    }

    if (Array.isArray(xml)) {
        return xml.forEach((v) => printNode(v, key));
    }

    if (typeof xml === 'object') {
        return Object.entries(xml).forEach(([key, v]) => printNode(v, key));
    }

    console.log(`${key}:${xml}`);
}