使用 java 将 XML 文件转换为 CSV 文件
我需要帮助了解使用java将XML文件转换为CSV文件所涉及的步骤。下面是 XML 文件的示例
<?xml version="1.0"?>
<Sites>
<Site id="101" name="NY-01" location="New York">
<Hosts>
<Host id="1001">
<Host_Name>srv001001</Host_Name>
<IP_address>10.1.2.3</IP_address>
<OS>Windows</OS>
<Load_avg_1min>1.3</Load_avg_1min>
<Load_avg_5min>2.5</Load_avg_5min>
<Load_avg_15min>1.2</Load_avg_15min>
</Host>
<Host id="1002">
<Host_Name>srv001002</Host_Name>
<IP_address>10.1.2.4</IP_address>
<OS>Linux</OS>
<Load_avg_1min>1.4</Load_avg_1min>
<Load_avg_5min>2.5</Load_avg_5min>
<Load_avg_15min>1.2</Load_avg_15min>
</Host>
<Host id="1003">
<Host_Name>srv001003</Host_Name>
<IP_address>10.1.2.5</IP_address>
<OS>Linux</OS>
<Load_avg_1min>3.3</Load_avg_1min>
<Load_avg_5min>1.6</Load_avg_5min>
<Load_avg_15min>1.8</Load_avg_15min>
</Host>
<Host id="1004">
<Host_Name>srv001004</Host_Name>
<IP_address>10.1.2.6</IP_address>
<OS>Linux</OS>
<Load_avg_1min>2.3</Load_avg_1min>
<Load_avg_5min>4.5</Load_avg_5min>
<Load_avg_15min>4.2</Load_avg_15min>
</Host>
</Hosts>
</Site>
</Sites>
这是生成的 CSV 文件。
site_id, site_name, site_location, host_id, host_name, ip_address, operative_system, load_avg_1min, load_avg_5min, load_avg_15min
101, NY-01, New York, 1001, srv001001, 10.1.2.3, Windows, 1.3, 2.5, 1.2
101, NY-01, New York, 1002, srv001002, 10.1.2.4, Linux, 1.4, 2.5, 1.2
101, NY-01, New York, 1003, srv001003, 10.1.2.5, Linux, 3.3, 1.6, 1.8
101, NY-01, New York, 1004, srv001004, 10.1.2.6, Linux, 2.3, 4.5, 4.2
我正在考虑使用DOM解析器来读取xml文件。我遇到的问题是,我需要按名称将特定元素指定为代码,但我希望它能够在不这样做的情况下解析它。
Java中是否有任何工具或库能够帮助我实现这一目标。
如果我在下面有一个这种格式的XML文件,并且想要将InitgPty的值与MSgId添加到同一行中(请注意:InitgPty在下一个标签级别,因此它将该值打印在下一行中)
<?xml version="1.0"?>
<CstmrCdtTrfInitn>
<GrpHdr>
<MsgId>XYZ07/ABC</MsgId>
<NbOfTxs>100000</NbOfTxs>
<InitgPty>
<Nm>XYZ</Nm>
</InitgPty>