使用 JAVA 计算、转换 XML 并将其编译为 CSV

发布于 2024-12-21 14:46:13 字数 1904 浏览 3 评论 0原文

我需要将多个 XML 文件(标准格式)转换并编译为单个 CSV 文件。因为我还需要对某些导入的元素执行计算,所以 XSLT 不是一个选项(Stackoverflow: XML 到 CSV 使用 XSLT),除非我对每个转换后的 CSV 文件执行计算。

有人建议使用 XPath 作为 SAX2 的替代方案,但由于最终的 CSV 输出很大(基于 100 多个 XML 文件),我对使用数组犹豫不决。 (Stackoverflow:将 XML 文件转换为 CSV

使用 SAX2 我在提取方面取得了一些成功标签元素。

如果我可以将每个单独文件的输出附加到最终的 CSV 输出,我认为我将拥有一个内存更稳定的应用程序。

我希望其他人能从了解这个问题的答案中受益:如何有效地处理大规模数据的计算以及 XML-CSV 转换?

XML 文件 1

<element id="1">
    <info>Yes</info>
    <startValue>0</startValue> <!-- Value entered twice, ignore--!>
    <startValue>256</startValue>
    <stopValue>64</stopValue>
</element>
<element id="2">
    <info>No</info>
    <startValue>50</startValue>
    <stopValue>25</stopValue>
</element>
<....

XML 文件 2

<element id="1">
    <info>No</info>
    <startValue>128</startValue>
    <stopValue>100</stopValue>
</element>    
<....

伪伪代码

for all files

    get ID
    get info

    for all stop and start values
        ignore wrong values: use counter
        difference[] = startValue(i) - stopValues(j) = 196, 28

    append (ID, info and difference) to file "outputfile.csv"

CSV 输出示例

File    ID  Info    Difference  Etc
_________________________________________________ 
0       1   Yes     196         ....
0       2   No      25          ....
1       1   No      28          ....
.           ...     ...         ....
.           ...     ...         ....
nfiles

I need to convert and compile multiple XML files (in a standard format) to a single CSV file. Because I also need to perform computations on some of the imported elements, XSLT is not an option (Stackoverflow: XML to CSV Using XSLT) unless I perform computations on each converted CSV file.

XPath has been suggested as an alternative to SAX2, but because the final CSV output is large (based on over 100 XML files) I am hesitant to use arrays. (Stackoverflow: Convert XML file to CSV)

Using SAX2 I have been somewhat successful in extracting the tag elements.

If I could append output - for each individual file - to the final CSV output I assume that I would have a more memory stable application.

I hope others would benefit from knowing the answer to the question: How can I efficiently handle computations in conjunction with XML-CSV conversions for large-scale data?

XML file 1

<element id="1">
    <info>Yes</info>
    <startValue>0</startValue> <!-- Value entered twice, ignore--!>
    <startValue>256</startValue>
    <stopValue>64</stopValue>
</element>
<element id="2">
    <info>No</info>
    <startValue>50</startValue>
    <stopValue>25</stopValue>
</element>
<....

XML file 2

<element id="1">
    <info>No</info>
    <startValue>128</startValue>
    <stopValue>100</stopValue>
</element>    
<....

Pseudopseudocode

for all files

    get ID
    get info

    for all stop and start values
        ignore wrong values: use counter
        difference[] = startValue(i) - stopValues(j) = 196, 28

    append (ID, info and difference) to file "outputfile.csv"

CSV Eutput Example

File    ID  Info    Difference  Etc
_________________________________________________ 
0       1   Yes     196         ....
0       2   No      25          ....
1       1   No      28          ....
.           ...     ...         ....
.           ...     ...         ....
nfiles

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

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

发布评论

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

评论(1

狼性发作 2024-12-28 14:46:13

我建议使用 JDOM 将 XML 读入内存。然后您可以使用常规 Java 语法以编程方式非常轻松地访问它。之后,您可以使用任何库轻松创建 CSV 文件。我个人使用 opencsv

如果您关心的是内存使用情况,那么最重要的是一次在内存中保留尽可能少的 XML 文件。如果您逐一读取文件,然后仅将所需的信息存储在其他数据结构中,那么应该没问题。例如,您可以创建一个由 ID 键入的起始值的映射和一个由 ID 键入的停止值的映射。

I would recommend using JDOM to read the XML into memory. Then you can very easily access it programmatically using regular Java syntax. After that, you can use any library to easily create a CSV file. Personally I use opencsv.

If your concern is memory usage, the biggest thing is to keep as few XML files in memory at one time as possible. If you read the files one by one and then store only the information you need in some other data structure, you should be fine. For example, you could create a Map of start values keyed by ID and a Map of stop values keyed by ID.

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