我有以下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程序,允许用户选择起始范围和结束范围,以便只有那些数据点最终出现在新文件中。例如,假设用户想要第二个值到第6个值,那么新的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>
实现这一目标的最佳方法是什么?有些人建议使用Arrays,而其他人列出,所以有点困惑。
此致 安东尼。
我有一些代码,但从我解析的不同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;
如果有任何帮助吗?
答案 0 :(得分:1)
基本上你需要做以下事情:
InputStream
(文件,网络......)如果你提到要使用哪个XML库,那么给你一些代码片段会更容易。
答案 1 :(得分:0)
了解Java读取XML文件的可能性。我猜这会对你有所帮助。 http://java.sun.com/developer/codesamples/xml.html
答案 2 :(得分:0)
对于数组与列表,它只取决于您将如何实现排序操作。我通常更喜欢List,然后我实现了Comparator来进行排序操作。根据您在问题中显示的数据样本,我的理解是您需要先按时间戳字段对所有数据进行排序,然后从该排序列表中返回一系列值。
我目前正在使用JAXB基于XML模式生成Java对象,然后XML到Java的转换是微不足道的。我不确定您是否已经有将XML文件读入对象的方法。您是否有XML文件的架构定义?如果你这样做,那么JAXB将很容易使用。