将 .csv 文件转换为 xml 或 json 时保留层次结构

发布于 2024-07-21 07:27:54 字数 1211 浏览 8 评论 0原文

我有一个关于将数据从 CSV 转换为 XML 或 JSON 的问题,其中保留数据的层次结构至关重要。

例如,如果我有这样的 CSV 数据:

type,brand,country,quantity
apple,golden_delicious,english,1
apple,golden_delicious,french,2
apple,cox,,4
apple,braeburn,,1
banana,,carribean,6
banana,,central_america,7
clememtine,,,3

我想要的是在 XML 中保留层次结构,以便我得到类似的内容:

<fruit>
<type = "apple">
<brand = "golden_delicious">
<country = "english" quantity =  "1">
<country = "french" quantity =  "2">
</brand>
<brand = "cox">
<quantity =  "4">
</brand>
<brand = "braeburn">
<quantity =  "1">
</brand>
</type>
<type = "banana">
<country = "carribean" quantity =  "6">
<country = "central_america" quantity =  "7">
</type>
<type = "clementine">
<quantity =  "3">
</type>
<fruit />

最好尝试使用 JAXP 或将上面的内容转换为简单的父项、子项和子项的表然后将数据写入字符串数组进行处理,? 像这样:

parent,child 
fruit,apple
apple,golden_delicious
golden_delicious,english
golden_delicious,french
english,1
french,2
apple,cox
cox,4
apple,braeburn
braeburn,1

等等。

或者,还有更好的方法?

谢谢西蒙

·莱文森

I have a question concerning translating data from a CSV into XML or JSON where it is essential to preserve the heirarchy of the data.

For example, if I have CSV data like this:

type,brand,country,quantity
apple,golden_delicious,english,1
apple,golden_delicious,french,2
apple,cox,,4
apple,braeburn,,1
banana,,carribean,6
banana,,central_america,7
clememtine,,,3

What I want is to preserve hierarchy in the XML so that I get something like:

<fruit>
<type = "apple">
<brand = "golden_delicious">
<country = "english" quantity =  "1">
<country = "french" quantity =  "2">
</brand>
<brand = "cox">
<quantity =  "4">
</brand>
<brand = "braeburn">
<quantity =  "1">
</brand>
</type>
<type = "banana">
<country = "carribean" quantity =  "6">
<country = "central_america" quantity =  "7">
</type>
<type = "clementine">
<quantity =  "3">
</type>
<fruit />

Is it best to try to use JAXP or to convert the above into a table simply of parent, child and then writing the data to an array of strings for processing,? Like this:

parent,child 
fruit,apple
apple,golden_delicious
golden_delicious,english
golden_delicious,french
english,1
french,2
apple,cox
cox,4
apple,braeburn
braeburn,1

And so on.

Or is there a better way?

Thanks

Simon Levinson

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-07-28 07:27:54

如果 csv 未按顺序排序会发生什么?

无论如何,当尝试解析 csv 文件时,请使用: http://www.codeproject.com /KB/database/CsvReader.aspx

它非常快速且易于使用

R

what happens if the csv is not sorted in order?

Anyhow, when trying to parse through csv files use: http://www.codeproject.com/KB/database/CsvReader.aspx

it's quite fast and easy to use

R

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文