xml-flow NPM 包 - 意外的 XML 解析行为 [英] xml-flow NPM package - Unexpected XML Parsing Behaviour

查看:79
本文介绍了xml-flow NPM 包 - 意外的 XML 解析行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xml-flow npm 包来使用流解析 XML.问题是 xml 节点以一种意想不到的方式被解析.

I am using xml-flow npm package to parse XML using streams. Issue is that the xml nodes are getting parsed in an unexpected way.

我的目的是使用重复的 xml 节点解析一个巨大的 XML 文件.XML 文件可以是任何 URL,重复节点将从 UI 提供.

My intention is to parse a huge XML file using a repeating xml node. The XML file can be any URL and the repeating node will be provided from UI.

我尝试使用具有所有可能值的选项,但解析行为似乎没有改变.

I tried to use the options with all possible values but the parsing behaviour doesn't seem to change.

我使用了以下示例 XML -

I used following sample XML -

<list>
    <item>
        <details>
            <id>1</id>
        </details>
    </item>
    <item>
        <details>
            <id>2</id>
            <description>description for item 2</description>
        </details>
    </item>
</list>

我尝试使用 item 作为重复节点来解析它,如下所示 -

I tried to parse it using item as repeating node as follows -

const fs = require("fs");
const flow = require("xml-flow");

const xmlStream = flow(fs.createReadStream("./sample.xml"));

xmlStream.on('tag:item', function (person) {
    console.log(JSON.stringify(person, null, 4));
});

我收到了 2 个解析的 xml 节点的以下响应 -

I got following response for 2 parsed xml nodes -

// node 1
{
    "$name": "item",
    "details": "1"
}

// node 2
{
    "$name": "item",
    "details": {        
        "id": "2",
        "description": "description for item 2"
    }
}

问题

正如您在响应中看到的,我为解析的 XML 节点获得了不同的 JSON 结构.

Problem

As you can see in the response, I get a different JSON structure for parsed XML nodes.

在第一个 XML 节点的情况下, 节点没有出现在 JSON 对象中(与第二个 XML 节点不同),因为它的父节点即.

只有一个子节点,即..

In case of first XML node, <id> node didn't appear in JSON object (unlike second XML node) because its parent node viz. <details> has only one child node viz. <id>.

这会导致我的应用程序出现问题,因为解析的 XML 可能有数千条记录 &由于这种行为,JSON 结构中到叶节点的相对路径正在发生变化.

This is causing problems in my application as the parsed XML might have thousands of records & the relative path in JSON structure to the leaf nodes are changing because of this behaviour.

例如,如果 xml 文件中有 10000 条记录,并且第 5000 条记录之后的所有记录都具有节点 2 结构,则 item.details 相对路径将指向记录 1 到 5000 的字符串,而相同的路径将指向剩余记录的对象.

As an example, if there are 10000 records in xml file and all the records after 5000th record have node 2 structure, item.details relative path will point to a string for records 1 to 5000 whereas the same path will point to an object for remaining records.

我确实尝试使用 xml-stream,它适用于相同的逻辑,但它带来了收集子项 在这里解释 这对我来说是更复杂的问题,因为在这种情况下传入的 XML 结构会因文件而异.

I did try to use xml-stream which works on the same logic, but it comes with a problem of collecting the sub-items explained here which is even more complicated problem for me as incoming XML structure in this case will vary from file to file.

如果我需要提供更多信息,请告诉我.

Let me know if I need to provide more information.

干杯!

推荐答案

好吧!在完成这些包的实现之后,除非提供明确的支持,否则似乎没有解决此问题的方法(我可能错过了一些东西).

Well! After going through the implementation of these packages, it seems there is no workaround for this problem (I might have missed something) unless explicit support is provided.

我终于决定写一个新的逻辑&最终编写了一个新的 npm 包 xtreamer 它提供了 xml 节点,而不是将它们转换为 JSON 对象.

I finally decided to write a new logic & ended up writing a new npm package xtreamer which provides xml nodes instead of converting them into JSON objects.

这个包公开了一个转换流,它可以通过任何可读流进行管道传输.它期望请求中的 xml 节点名称并发出自定义事件 xmldata 以输出 xml 节点.

This package exposes a transform stream that can be piped with any readable stream. It expects xml node name in request and emits a custom event xmldata to output the xml node.

可以根据需要将输出插入任何 xml-json npm 包 以获得最终的 JSON.检查 npm 包以获取更多详细信息.

The output can the be plugged in to any xml-json npm package as per the requirement to get the final JSON. Check the npm package for further details.

支持模块

我设法创建了另一个 npm 包 xtagger 它使用 sax npm 包 并提供以下格式的 xml 结构 -

I managed to create one more npm package xtagger which uses sax npm package and provides xml structure in following format -

structure: { [name: string]: { [hierarchy: number]: number } };

此包可用于通过考虑层次结构来查找 xml 文件中的重复节点.

This package can be used to find the repeating nodes in xml file by considering their hierarchy.

这篇关于xml-flow NPM 包 - 意外的 XML 解析行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆