我的 playbook 大部分都起作用了.它将我的XML转换为json.问题是,id
属性丢失了.
脚本:
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);
}
}
我是这样称呼它的
const XMLDoc = (new DOMParser()).parseFromString(XMLString, "text/xml");
console.log(JSON.stringify(xml2js(XMLDoc), null, "\t"));
我的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>
预期yields
{
"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>"
}
}
]
}
}
实际yields
{
"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>"
}
}
]
}
}
注意应该如何按照预期将id
元素添加到topic
个对象中,但却错误地将其添加到顶级对象中.