gpt4 book ai didi

XML to JSON conversion dropping attributes(从XML到JSON的转换删除属性)

转载 作者:bug小助手 更新时间:2023-10-25 20:34:50 32 4
gpt4 key购买 nike



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>&lt;a href=&quot;http://localhost/SMF2.1/index.php?topic=78603.0&quot;&gt;Automated Test #14561&lt;/a&gt;&lt;br&gt;Started by &lt;a href=&quot;http://localhost/SMF2.1/index.php?action=profile;u=10&quot;&gt;Member 10&lt;/a&gt; </subject>
<board>&lt;a href=&quot;http://localhost/SMF2.1/index.php?board=51.0&quot;&gt;Board Number 51&lt;/a&gt;</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>&lt;a href=&quot;http://localhost/SMF2.1/index.php?topic=78604.0&quot;&gt;Automated Test #14562&lt;/a&gt;&lt;br&gt;Started by &lt;a href=&quot;http://localhost/SMF2.1/index.php?action=profile;u=10&quot;&gt;Member 10&lt;/a&gt; </subject>
<board>&lt;a href=&quot;http://localhost/SMF2.1/index.php?board=51.0&quot;&gt;Board Number 51&lt;/a&gt;</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>&lt;a href=&quot;http://localhost/SMF2.1/index.php?topic=78602.0&quot;&gt;Automated Test #14560&lt;/a&gt;&lt;br&gt;Started by &lt;a href=&quot;http://localhost/SMF2.1/index.php?action=profile;u=10&quot;&gt;Member 10&lt;/a&gt; </subject>
<board>&lt;a href=&quot;http://localhost/SMF2.1/index.php?board=51.0&quot;&gt;Board Number 51&lt;/a&gt;</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.

属性不是子节点,它们是“属性”节点。你需要这样对待他们。

@Nick correct..

@尼克答对了..

优秀答案推荐


  • Rearrange the code so the attributes are added to a current node, not a parent one, plus you need to add attributes to the siblings also.

    重新排列代码,以便将属性添加到当前节点,而不是父节点,此外还需要将属性添加到同级节点。



  • Take advantage of iterators to loop through children and attributes.

    利用迭代器对子对象和属性进行循环。



  • Also you could have some design flaw when a text node doesn't have attributes. Maybe that's ok.

    此外,当文本节点没有属性时,您可能会有一些设计缺陷。也许这没什么问题。






const XMLDoc = (new DOMParser()).parseFromString(XMLString, "text/xml");
$pre.textContent = JSON.stringify(xml2js(XMLDoc), null, 4);

function xml2js(node){
try {

// collect attributes first
const obj = [...node.attributes || []].reduce((r, attr) => (r[attr.nodeName] = attr.value, r), {});

if (!node.children.length) {
obj.content = node.textContent;
} else

for (const item of node.children){
const {nodeName} = item, child = obj[nodeName];
child ?
(Array.isArray(child) ? child : obj[nodeName] = [child]).push(xml2js(item)) :
obj[nodeName] = xml2js(item);
}

return obj;

} catch (e) {
console.log(e.message);
}
}

<script>
const XMLString = `<?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>&lt;a href=&quot;http://localhost/SMF2.1/index.php?topic=78603.0&quot;&gt;Automated Test #14561&lt;/a&gt;&lt;br&gt;Started by &lt;a href=&quot;http://localhost/SMF2.1/index.php?action=profile;u=10&quot;&gt;Member 10&lt;/a&gt; </subject>
<board>&lt;a href=&quot;http://localhost/SMF2.1/index.php?board=51.0&quot;&gt;Board Number 51&lt;/a&gt;</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>&lt;a href=&quot;http://localhost/SMF2.1/index.php?topic=78604.0&quot;&gt;Automated Test #14562&lt;/a&gt;&lt;br&gt;Started by &lt;a href=&quot;http://localhost/SMF2.1/index.php?action=profile;u=10&quot;&gt;Member 10&lt;/a&gt; </subject>
<board>&lt;a href=&quot;http://localhost/SMF2.1/index.php?board=51.0&quot;&gt;Board Number 51&lt;/a&gt;</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>&lt;a href=&quot;http://localhost/SMF2.1/index.php?topic=78602.0&quot;&gt;Automated Test #14560&lt;/a&gt;&lt;br&gt;Started by &lt;a href=&quot;http://localhost/SMF2.1/index.php?action=profile;u=10&quot;&gt;Member 10&lt;/a&gt; </subject>
<board>&lt;a href=&quot;http://localhost/SMF2.1/index.php?board=51.0&quot;&gt;Board Number 51&lt;/a&gt;</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>`
</script>
<pre id="$pre"></pre>




更多回答

ts attribute under time is dropped but must be kept.

TIME下的TS属性被删除,但必须保留。

@live627 thrn textContent sould have a name and present always or only when attributes exist. you didnt described that

@live627到TextContent应该有一个名称,并始终或仅在存在属性时显示。你没有描述过那个

Always; would probably be better to be consistent.

总是这样;保持一致可能会更好。

@live627 fixed....

@live627已修复...

one final question: I'm unfamiliar with some syntax in const {nodeName} = item, child = obj[nodeName];, specifically item, child

最后一个问题:我不熟悉const{nodeName}=Item,Child=obj[nodeName];中的一些语法,特别是Item,Child

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com