【发布时间】:2014-06-05 16:18:29
【问题描述】:
正在尝试获取 XML..
<user-types>
<section id="11" handle="user-types">User types</section>
<entry id="9">
<name mode="unformatted" handle="home-owner" word-count="2" lang="en" handle-en="home-owner"><![CDATA[Home owner]]></name>
</entry>
<entry id="7">
<name mode="unformatted" handle="contractor-residential" word-count="2" lang="en" handle-en="contractor-residential"><![CDATA[Contractor (residential)]]></name>
</entry>
<entry id="8">
<name mode="unformatted" handle="contractor-non-residential" word-count="2" lang="en" handle-en="contractor-non-residential"><![CDATA[Contractor (non-residential)]]></name>
</entry>
<entry id="10">
<name mode="unformatted" handle="commercial-property-owner" word-count="3" lang="en" handle-en="commercial-property-owner"><![CDATA[Commercial property owner]]></name>
</entry>
</user-types>
是 JSON,但是标签 entry 没有变成一个数组,所以 JS 只能读取其中一个:(
当前损坏的 JSON 输出
{
"user-types" : {
"section" : {
"@attributes" : {
"id" : 11,
"handle" : "user-types"
},
"value" : "User types"
},
"entry" : {
"@attributes" : {
"id" : 9
},
"name" : {
"@attributes" : {
"mode" : "unformatted",
"handle" : "home-owner",
"word-count" : 2,
"lang" : "en",
"handle-en" : "home-owner"
},
"value" : "Home owner"
}
},
"entry" : {
"@attributes" : {
"id" : 7
},
"name" : {
"@attributes" : {
"mode" : "unformatted",
"handle" : "contractor-residential",
"word-count" : 2,
"lang" : "en",
"handle-en" : "contractor-residential"
},
"value" : "Contractor (residential)"
}
},
"entry" : {
"@attributes" : {
"id" : 8
},
"name" : {
"@attributes" : {
"mode" : "unformatted",
"handle" : "contractor-non-residential",
"word-count" : 2,
"lang" : "en",
"handle-en" : "contractor-non-residential"
},
"value" : "Contractor (non-residential)"
}
},
"entry" : {
"@attributes" : {
"id" : 10
},
"name" : {
"@attributes" : {
"mode" : "unformatted",
"handle" : "commercial-property-owner",
"word-count" : 3,
"lang" : "en",
"handle-en" : "commercial-property-owner"
},
"value" : "Commercial property owner"
}
}
}
}
使用这个 XSL https://github.com/iwyg/xml-to-json
【问题讨论】:
-
您可以在json.parser.online.fr上测试结果
-
我在这里没有看到任何计算机程序。您能否展示您正在尝试改进的将 XML 转换为 JSON 的代码?
-
使用这个 XSL github.com/iwyg/xml-to-json
-
我无法使用您指向的样式表重现您的结果(我看到的实际输出比您报告的要糟糕得多 - xsltransform.net/eiQZDbv)。我建议您寻找更好的来源来复制您的代码。