My script works for the most part... it converts my xml into json. The problem is that the id
attribute gets lost.
我的剧本大部分都起作用了.它将我的XML转换为json。问题是id属性丢失了。
The script:
脚本:
function xml2js(xml)
{
try
{
let obj = {};
if (xml.children.length == 0)
obj = xml.textContent;
else
{
for (let i = 0; i < xml.children.length; i++)
{
let item = xml.children.item(i);
let nodeName = item.nodeName;
if (obj[nodeName])
{
if (typeof (obj[nodeName].push) == "undefined")
{
let old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xml2js(item));
}
else
{
if (item.attributes)
for (let attribute of item.attributes)
obj[attribute.nodeName] = attribute.nodeValue;
obj[nodeName] = xml2js(item);
}
}
}
return obj;
}
catch (e)
{
console.log(e.message);
}
}
I call it like so
我是这样称呼它的
const XMLDoc = (new DOMParser()).parseFromString(XMLString, "text/xml");
console.log(JSON.stringify(xml2js(XMLDoc), null, "\t"));
My xml
我的XML
<?xml version="1.0" encoding="UTF-8"?>
<smf>
<lastTime>1694246241</lastTime>
<topic id="78603">
<icon><![CDATA[<div class="board_icon"><img src="http://localhost/SMF2.1/Themes/default/images/post/xx.png" alt=""></div>]]></icon>
<subject><a href="http://localhost/SMF2.1/index.php?topic=78603.0">Automated Test #14561</a><br>Started by <a href="http://localhost/SMF2.1/index.php?action=profile;u=10">Member 10</a> </subject>
<board><a href="http://localhost/SMF2.1/index.php?board=51.0">Board Number 51</a></board>
<replies><![CDATA[7 Replies<br>8 Views]]>></replies>
<lastPost>
<time ts="1694246241"><![CDATA[<strong>Today</strong> at 12:57 AM]]></time>
<link><![CDATA[by <a href="http://localhost/SMF2.1/index.php?action=profile;u=1">live627</a>D <a href="http://localhost/SMF2.1/index.php?topic=78603.new;topicseen#new" class="new_posts">New</a>]]></link>
</lastPost>
</topic>
<topic id="78604">
<icon><![CDATA[<div class="board_icon"><img src="http://localhost/SMF2.1/Themes/default/images/post/xx.png" alt=""></div>]]></icon>
<subject><a href="http://localhost/SMF2.1/index.php?topic=78604.0">Automated Test #14562</a><br>Started by <a href="http://localhost/SMF2.1/index.php?action=profile;u=10">Member 10</a> </subject>
<board><a href="http://localhost/SMF2.1/index.php?board=51.0">Board Number 51</a></board>
<replies><![CDATA[0 Replies<br>0 Views]]>></replies>
<lastPost>
<time ts="1693697618"><![CDATA[Sep 02, 2023, 04:33 PM]]></time>
<link><![CDATA[by <a href="http://localhost/SMF2.1/index.php?action=profile;u=10">Member 10</a>D <a href="http://localhost/SMF2.1/index.php?topic=78604.new;topicseen#new" class="new_posts">New</a>]]></link>
</lastPost>
</topic>
<topic id="78602">
<icon><![CDATA[<div class="board_icon"><img src="http://localhost/SMF2.1/Themes/default/images/post/xx.png" alt=""></div>]]></icon>
<subject><a href="http://localhost/SMF2.1/index.php?topic=78602.0">Automated Test #14560</a><br>Started by <a href="http://localhost/SMF2.1/index.php?action=profile;u=10">Member 10</a> </subject>
<board><a href="http://localhost/SMF2.1/index.php?board=51.0">Board Number 51</a></board>
<replies><![CDATA[0 Replies<br>0 Views]]>></replies>
<lastPost>
<time ts="1693697618"><![CDATA[Sep 02, 2023, 04:33 PM]]></time>
<link><![CDATA[by <a href="http://localhost/SMF2.1/index.php?action=profile;u=10">Member 10</a>D <a href="http://localhost/SMF2.1/index.php?topic=78602.new;topicseen#new" class="new_posts">New</a>]]></link>
</lastPost>
</topic>
</smf>
Expected output
预期产量
{
"smf": {
"lastTime": "1694246241",
"topic": [
{
"id": "78603",
"icon": "<div class=\"board_icon\"><img src=\"http://localhost/SMF2.1/Themes/default/images/post/xx.png\" alt=\"\"></div>",
"subject": "<a href=\"http://localhost/SMF2.1/index.php?topic=78603.0\">Automated Test #14561</a><br>Started by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a> ",
"board": "<a href=\"http://localhost/SMF2.1/index.php?board=51.0\">Board Number 51</a>",
"replies": "7 Replies<br>8 Views>",
"lastPost": {
"ts": "1694246241",
"time": "Sep 09, 2023, 12:57 AM",
"link": "by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=1\">live627</a>D <a href=\"http://localhost/SMF2.1/index.php?topic=78603.new;topicseen#new\" class=\"new_posts\">New</a>"
}
},
{
"id": "78604",
"icon": "<div class=\"board_icon\"><img src=\"http://localhost/SMF2.1/Themes/default/images/post/xx.png\" alt=\"\"></div>",
"subject": "<a href=\"http://localhost/SMF2.1/index.php?topic=78604.0\">Automated Test #14562</a><br>Started by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a> ",
"board": "<a href=\"http://localhost/SMF2.1/index.php?board=51.0\">Board Number 51</a>",
"replies": "0 Replies<br>0 Views>",
"lastPost": {
"ts": "1693697618",
"time": "Sep 02, 2023, 04:33 PM",
"link": "by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a>D <a href=\"http://localhost/SMF2.1/index.php?topic=78604.new;topicseen#new\" class=\"new_posts\">New</a>"
}
},
{
"id": "78602",
"icon": "<div class=\"board_icon\"><img src=\"http://localhost/SMF2.1/Themes/default/images/post/xx.png\" alt=\"\"></div>",
"subject": "<a href=\"http://localhost/SMF2.1/index.php?topic=78602.0\">Automated Test #14560</a><br>Started by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a> ",
"board": "<a href=\"http://localhost/SMF2.1/index.php?board=51.0\">Board Number 51</a>",
"replies": "0 Replies<br>0 Views>",
"lastPost": {
"ts": "1693697618",
"time": "Sep 02, 2023, 04:33 PM",
"link": "by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a>D <a href=\"http://localhost/SMF2.1/index.php?topic=78602.new;topicseen#new\" class=\"new_posts\">New</a>"
}
}
]
}
}
Actual output
实际产量
{
"smf": {
"lastTime": "1694246241",
"id": "78603",
"topic": [
{
"icon": "<div class=\"board_icon\"><img src=\"http://localhost/SMF2.1/Themes/default/images/post/xx.png\" alt=\"\"></div>",
"subject": "<a href=\"http://localhost/SMF2.1/index.php?topic=78603.0\">Automated Test #14561</a><br>Started by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a> ",
"board": "<a href=\"http://localhost/SMF2.1/index.php?board=51.0\">Board Number 51</a>",
"replies": "7 Replies<br>8 Views>",
"lastPost": {
"ts": "1694246241",
"time": "<strong>Today</strong> at 12:57 AM",
"link": "by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=1\">live627</a>D <a href=\"http://localhost/SMF2.1/index.php?topic=78603.new;topicseen#new\" class=\"new_posts\">New</a>"
}
},
{
"icon": "<div class=\"board_icon\"><img src=\"http://localhost/SMF2.1/Themes/default/images/post/xx.png\" alt=\"\"></div>",
"subject": "<a href=\"http://localhost/SMF2.1/index.php?topic=78604.0\">Automated Test #14562</a><br>Started by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a> ",
"board": "<a href=\"http://localhost/SMF2.1/index.php?board=51.0\">Board Number 51</a>",
"replies": "0 Replies<br>0 Views>",
"lastPost": {
"ts": "1693697618",
"time": "Sep 02, 2023, 04:33 PM",
"link": "by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a>D <a href=\"http://localhost/SMF2.1/index.php?topic=78604.new;topicseen#new\" class=\"new_posts\">New</a>"
}
},
{
"icon": "<div class=\"board_icon\"><img src=\"http://localhost/SMF2.1/Themes/default/images/post/xx.png\" alt=\"\"></div>",
"subject": "<a href=\"http://localhost/SMF2.1/index.php?topic=78602.0\">Automated Test #14560</a><br>Started by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a> ",
"board": "<a href=\"http://localhost/SMF2.1/index.php?board=51.0\">Board Number 51</a>",
"replies": "0 Replies<br>0 Views>",
"lastPost": {
"ts": "1693697618",
"time": "Sep 02, 2023, 04:33 PM",
"link": "by <a href=\"http://localhost/SMF2.1/index.php?action=profile;u=10\">Member 10</a>D <a href=\"http://localhost/SMF2.1/index.php?topic=78602.new;topicseen#new\" class=\"new_posts\">New</a>"
}
}
]
}
}
Notice how the id
element should be added to the topic
objects as is expected but is instead wrongly added to the top-level object.
注意id元素应该如何像预期的那样添加到TOPIC对象,但却错误地添加到顶级对象。
更多回答
If id
should be inside topic
, then to be consistent ts
should be inside time
如果id应该在主题内,那么为了保持一致,ts应该在时间内
Attributes are not children nodes, they are "attribute" nodes. You need to treat them as such.
属性不是子节点,它们是“属性”节点。你需要这样对待他们。
我是一名优秀的程序员,十分优秀!