将 .csv 文件转换为 xml 或 json 时保留层次结构
我有一个关于将数据从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 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