gpt4 book ai didi

Java程序编辑文件以选择记录

转载 作者:行者123 更新时间:2023-12-01 16:01:59 24 4
gpt4 key购买 nike

我有以下 XML 文件:

<graph caption="ECG Data Wave" subcaption="For Person's Name" xAxisName="Time" yAxisMinValue="-0.025" yAxisName="Voltage" decimalPrecision="5" formatNumberScale="0" numberPrefix="" showNames="1" showValues="0" showAlternateHGridColor="1" AlternateHGridColor="ff5904" divLineColor="ff5904" divLineAlpha="20" alternateHGridAlpha="5">
<set name="12:00:00.01" value="0.600000" hoverText = "The difference from last value: 0" ></set>
<set name="12:00:00.02" value="0.640000" hoverText = "The difference from last value: 0.04" ></set>
<set name="12:00:01.025" value="0.340000" hoverText = "The difference from last value: -0.3" ></set>
<set name="12:00:01.031" value="0.100000" hoverText = "The difference from last value: -0.24" ></set>
<set name="12:00:01.039" value="-0.100000" hoverText = "The difference from last value: -0.2" ></set>
<set name="12:00:01.050" value="-0.200000" hoverText = "The difference from last value: -0.1" ></set>
<set name="12:00:02.01" value="0.010000" hoverText = "The difference from last value: 0.21" ></set>
<set name="12:00:02.12" value="0.600000" hoverText = "The difference from last value: 0.59" ></set>
<set name="12:00:02.23" value="0.500000" hoverText = "The difference from last value: -0.1" ></set>
<set name="12:00:02.028" value="0.300000" hoverText = "The difference from last value: -0.2" ></set>
<set name="12:00:02.031" value="0.100000" hoverText = "The difference from last value: -0.2" ></set>
<set name="12:00:03.049" value="0.000000" hoverText = "The difference from last value: -0.1" ></set>
<set name="12:00:03.145" value="-0.050000" hoverText = "The difference from last value: -0.05" ></set>
<set name="12:00:04.12" value="0.110000" hoverText = "The difference from last value: 0.16" ></set>
<trendlines>
<line startvalue="0.30" displayValue="High Activity" color="FF0000" thickness="1" isTrendZone="0"></line>
<line startvalue="-0.05" displayValue="Low Activity" color="009999" thickness="1" isTrendZone="0"></line>
</trendlines>
</graph>

我想编写一个 Java 程序,允许用户选择起始范围和结束范围,以便只有这些数据点最终出现在新文件中。例如,假设用户想要第二个值到第六个值,因此新的 XML 文件如下:

<graph caption="ECG Data Wave" subcaption="For Person's Name" xAxisName="Time" yAxisMinValue="-0.025" yAxisName="Voltage" decimalPrecision="5" formatNumberScale="0" numberPrefix="" showNames="1" showValues="0" showAlternateHGridColor="1" AlternateHGridColor="ff5904" divLineColor="ff5904" divLineAlpha="20" alternateHGridAlpha="5">
<set name="12:00:00.02" value="0.640000" hoverText = "The difference from last value: 0.04" ></set>
<set name="12:00:01.025" value="0.340000" hoverText = "The difference from last value: -0.3" ></set>
<set name="12:00:01.031" value="0.100000" hoverText = "The difference from last value: -0.24" ></set>
<set name="12:00:01.039" value="-0.100000" hoverText = "The difference from last value: -0.2" ></set>
<set name="12:00:01.050" value="-0.200000" hoverText = "The difference from last value: -0.1" ></set>
<trendlines>
<line startvalue="0.30" displayValue="High Activity" color="FF0000" thickness="1" isTrendZone="0"></line>
<line startvalue="-0.05" displayValue="Low Activity" color="009999" thickness="1" isTrendZone="0"></line>
</trendlines>
</graph>

实现这一目标的最佳方法是什么?有些人建议使用数组,而另一些人则建议使用列表,所以有点困惑。

问候,安东尼。

嗯,我有一些代码,但说实话,从我解析的不同 XML 文件中得出的结果并不好,只是将数据返回到屏幕。它使用了

javax.xml.parsers.DocumentBuilder; 
javax.xml.parsers.DocumentBuilderFactory;
java.io.File;
org.w3c.dom.Document;
org.w3c.dom.Element;
org.w3c.dom.Node;
org.w3c.dom.NodeList;

这有什么帮助吗?

最佳答案

基本上您需要执行以下操作:

  • 打开 XML 文件,很可能从中创建一个 InputStream(文件、网络...)
  • 将其提供给 XML 解析器
  • 遍历节点并根据用户输入进行过滤
  • 或者,您可能想使用 XPath 来过滤节点

如果您提到您将使用哪个 XML 库,那么为您提供一些代码片段会更容易。

关于Java程序编辑文件以选择记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3626335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com