问题标签 [xml2js]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
71 浏览

javascript - 从nodejs xml2js'结果'中提取信息

使用 xml2js nodejs 模块,我设法使用以下代码从(大!)xml文档中提取;

这个“有效”,结果的一小部分摘录如下;

"dealer_id":["--"],"dealer_name":["-- 摩托车"],"vehicle_registration_plate":["--"],"fb_page_id":["--"],"dealer_communication_channel":[ "LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https://www.motorcyclenews.com/privacy-policy/"],"mileage":[{"value" :["552"],"unit":["MI"]}],"address":[{"$":{"format":"simple"},"component":[{" ":"Ivy路车库","$":{"name":"addr1"}},{" ":"Ivy Road","$":{"name":"city"}},{" ":"Lancashire" "$":{"name":"region"}},{" ":"UK","$":{"name":"country"}},{"":"-- 6DJ","$":{"name":"postal_code"}}]}],"vehicle_type":["MOTORCYCLE"],"dealer_url":["https://www.motorcyclenews. com/api/bfs-dealer/redirect/--"]},{"vehicle_id":["56968134"],"make":["HONDA"],"model":["NSS300 FORZA"],"year ":["2018"],"transmission":["OTHER"],"body_style":["OTHER"],"fuel_type":["GASOLINE"],"drivetrain":["RWD"],"description ":["HONDA FORZA 300CC ULTRA LOW MILES, TOP BOX FITTED, Hand Guards, Heated Grips, 完好无损, 为什么要上火车或公共汽车节省时间和金钱, 可在全国范围内提供低成本金融服务。"],"image": [{"url":["https://cdn.images.autoexposure.co.uk/AETA86362/AETV24605587_5.jpg?t=--"]}],"url":["https://www.motorcyclenews.com/bikes-for-sale/honda/nss300-forza /--/"],"title":["HONDA NSS300 FORZA (2018)"],"price":["3,750.00 GBP"],"state_of_vehicle":["USED"],"exterior_color":["BLACK "],"latitude":["51.7241967327"],"longitude":["0.4833483158"],"dealer_id":["-- City Motorcycles LTD"],"dealer_name":["-- City Motorcycles LTD"] ,"vehicle_registration_plate":["--"],"fb_page_id":["--"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url": [“https://www.motorcyclenews.com/privacy-policy/"],"mileage":[{"value":["1793"],"unit":["MI"]}],"address":[{"$":{ "格式":"简单"},"组件":[{"":"198-200 Baddow Road","$":{"name":"addr1"}},{" ":"Unknown","$":{"name":"city"}},{" ":"埃塞克斯","$":{"name":"region"}},{" ":"UK","$":{"name":"country"}},{" ":"CM2 9QP","$":{"name":"postal_code"}}]}],"vehicle_type":["MOTORCYCLE"],"dealer_url":["https://www.motorcyclenews.com/api/bfs -dealer/redirect/--"]},{"vehicle_id":["56970343"],"make":["HONDA"],"model":["VISION 110"],"year":["2011 "],"传输":["OTHER"],"body_style":["OTHER"],"fuel_type":["GASOLINE"],"drivetrain":["RWD"],"description":[""],"url":["https://www. motornews.com/bikes-for-sale/honda/vision-110/--

/"],"标题":["本田

VISION 110 (2011)"],"price":["1,250.00 GBP"],"state_of_vehicle":["USED"],"exterior_color":["Red"],"latitude":["52.2398688251"]," longitude":["0.752311123"],"dealer_id":["John Banks Honda Motorcycles"],"dealer_name":["John -- Honda Motorcycles"],"vehicle_registration_plate":["--"],"fb_page_id" :["7714743854"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https://www.motorcyclenews.com/privacy-policy/ "],"里程":[{"价值":["5051"],"单位":["MI"]}],"地址":[{"$":{"格式":"simple"},"component":[{" ":"Kempson Way","$":{"name":"addr1"}},{" ":"Moreton Way","$":{" name":"city"}},{" ":"Suffolk","$":{"name":"region"}},{" ":"UK","$":{"name":"国家”}},{”名称":"国家"}},{"名称":"国家"}},{"":"-- 7AR","$":{"name":"postal_code"}}]}],"vehicle_type":["MOTORCYCLE"],"dealer_url":["https://www.motorcyclenews. com/api/bfs-dealer/redirect/--"]},{"vehicle_id":["--"],"make":["HONDA"],"model":["CRF250"],"year ":["2019"],"transmission":["OTHER"],"body_style":["OTHER"],"fuel_type":["GASOLINE"],"drivetrain":["RWD"],"description ":[""],"url":["https://www.motorcyclenews.com/bikes-for-sale/honda/crf250/--/"],"title":["HONDA CRF250 (2019) "],"price":["4,500.00 GBP"],"state_of_vehicle":["USED"],"exterior_color":["Red"],"latitude":["52.2398688251"],"longitude":["0.752311123"],"dealer_id":["John Banks Honda Motorcycles"],"dealer_name":["John - - 本田摩托车"],"vehicle_registration_plate":["--"],"fb_page_id":["7714743854"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"], “dealer_privacy_policy_url”:[“https://www.motorcyclenews.com/privacy-policy/”],“里程”:[{“价值”:[“1000”],“单位”:[“MI”]}] ,"地址":[{"$":{"格式":"简单"},"组件":[{"longitude":["0.752311123"],"dealer_id":["John Banks Honda Motorcycles"],"dealer_name":["John -- Honda Motorcycles"],"vehicle_registration_plate":["--"],"fb_page_id" :["7714743854"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https://www.motorcyclenews.com/privacy-policy/ "],"里程":[{"价值":["1000"],"单位":["MI"]}],"地址":[{"$":{"格式":"简单"} ,“零件”:[{”longitude":["0.752311123"],"dealer_id":["John Banks Honda Motorcycles"],"dealer_name":["John -- Honda Motorcycles"],"vehicle_registration_plate":["--"],"fb_page_id" :["7714743854"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https://www.motorcyclenews.com/privacy-policy/ "],"里程":[{"价值":["1000"],"单位":["MI"]}],"地址":[{"$":{"格式":"简单"} ,“零件”:[{”vehicle_registration_plate":["--"],"fb_page_id":["7714743854"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https ://www.motorcyclenews.com/privacy-policy/"],"里程":[{"value":["1000"],"unit":["MI"]}],"address":[{ "$":{"格式":"简单"},"组件":[{"vehicle_registration_plate":["--"],"fb_page_id":["7714743854"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https ://www.motorcyclenews.com/privacy-policy/"],"里程":[{"value":["1000"],"unit":["MI"]}],"address":[{ "$":{"格式":"简单"},"组件":[{"["1000"],"unit":["MI"]}],"address":[{"$":{"format":"simple"},"component":[{"["1000"],"unit":["MI"]}],"address":[{"$":{"format":"simple"},"component":[{"":"Kempson Way","$":{"name":"addr1"}},{" ":"Moreton Way","$":{"name":"city"}},{" ": "萨福克","$":{"name":"region"}},{" ":"UK","$":{"name":"country"}},{" ":"-- 7AR ","$":{"name":"postal_code"}}]}],"vehicle_type":["MOTORCYCLE"],"dealer_url":["https://www.motorcyclenews.com/api/bfs-经销商/重定向/--"]}]}}

但我现在想做的是从中提取一些信息。

特别是,我想知道列表中出现了多少次,例如“vehicle_id”的条目。

然后我可以将它用于我测试的其他部分。

任何帮助将不胜感激。

谢谢

0 投票
1 回答
392 浏览

javascript - xml2js valueProcessor 删除 \t 和 \n

我在解析 XML 文件时遇到问题。我想删除带有 \t\n 等字符的字符串。

XML 文件: http: //ftp.thinkimmo.com/home/immoanzeigen24/immo.xml

清洁值:

我试着用我在网上找到的很多正则表达式来清理它——但值总是如下所示:

0 投票
3 回答
191 浏览

javascript - 使用异步获取分配后对象属性未定义

我在向对象添加键时遇到了一些麻烦,如下所示:

以下是 console.logs 的结果

我担心这个问题与异步调用有关,但我对为什么第一个console.log()工作正常感到困惑。如果是异步问题,我该如何解决?

0 投票
1 回答
344 浏览

node.js - 如何使用 xml2js 向数组元素添加属性?

我试图弄清楚如何将属性添加到数组元素

Error: Invalid character in name尝试从对象构建以下 XML 时出现错误。

这是我的对象

我也试过用引号括起来的名字

完全剥离属性声明会产生所需的 XML,但显然会遗漏所需的属性

0 投票
1 回答
555 浏览

vue.js - 使用 XML2JS 从 Nuxt 中的 API 解析 XML

我正在尝试使用 XML2JS 循环遍历 nuxt 中的 API,然后循环遍历数据以在 html 文件中显示它。

它在控制台记录时工作,但它没有在 HTML 中循环。它没有任何错误,只是空白,没有在 html 中显示任何内容。

JSON:

任何帮助将不胜感激,如果您需要我提供更多信息,请告诉我。

谢谢。

0 投票
2 回答
574 浏览

node.js - xml2js 返回的数组不保持所有顺序

当 xml 元素被捆绑时,一些顺序会丢失。

例如,我正在阅读的 xml 具有如下元素:

可以有任意数量的<w> something </w>条目和 0 个或多个<k>something</k>条目以及 0 个或多个<q> something </q>条目。

但我需要保持解析中的顺序。当我使用以下代码时:

我得到:

我想要的是

ETC

有什么方法可以维护这些信息的顺序吗?

0 投票
1 回答
403 浏览

javascript - 无法读取未定义承诺错误的属性“解析器”

我正在通过 API 解析一些数据,我的解析代码如下所示,

这是我的解析器代码,但我仍然收到控制台错误
core.js:4197 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'Parser' of undefined TypeError: Cannot read property 'Parser' of undefined

所以我的问题是代码有什么问题?当我返回一个承诺时,我还能做什么?我正在返回一个新的承诺,然后我调用了解析器,那么我怎么会出错呢?谁能帮我?

0 投票
2 回答
152 浏览

node.js - 使用节点 XML 解析器解析 XLIFF 的问题

我正在尝试使用 xml2js 库解析 XLIFF 文件。一切正常,但如果我有类似的东西:<source>Welcome to <x id="INTERPOLATION" equiv-text="{{ title }}"/> my friend</source>我会得到[{"_":"Welcome to my friend","x":[{"$":{"id":"INTERPOLATION","equiv-text":"{{ title }}"}}]}]. 我基本上失去了句子部分的顺序。我希望得到一个由 3 个部分组成的数组:

但相反,我得到:

如果我再次尝试重新创建字符串,我会得到<source>Welcome to my friend<x id="INTERPOLATION" equiv-text="{{ title }}"/></source>

知道如何用这个 XML 解析器或其他任何方法来解决它吗?

0 投票
1 回答
628 浏览

reactjs - 如何通过在 React JS 中浏览来读取 xml 文件

我正在尝试通过使用输入浏览本地机器来读取 XML 文件。

这是我的代码

在这里,我使用的确切路径仍然无法读取文件并出现此错误。

类型错误:fs__WEBPACK_IMPORTED_MODULE_3__.readFile 不是函数

0 投票
0 回答
40 浏览

javascript - 将特定键转换为对象数组而不是对象

我有一个返回 XML 响应的服务。我正在使用 xml2JS 将 XML 解析为 JSON。XML 看起来像 -

解析 JSON 后看起来像 -

键“children”可以有一条记录或多条记录。当键“children”有一个记录时,解析器将类型作为对象,当有多个记录时,它变成一个数组,据我所知,这是 xml2JS 解析器的工作方式。根据我的要求,我希望这个键是一个数组,不管它有多少记录。此外,这是一个数组,它可以嵌套到第 n 级,并且键“children”应该是一个对象数组,总是。搜索“儿童”:{ . 我希望这是“孩子”:[

这是一个示例响应。可以有多个类似的实例。任何人都可以帮助解决这个问题吗?