I have the following XML file:
<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" ></s开发者_如何学Goet>
<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>
I want to write a Java program that will allow users to select a starting range and an end range so that only those data points end up in the new file. For example, suppose the user wants the 2nd value to the 6th value so the new XML File is as follows:
<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>
How is the best way to achieve this? Some people have suggested using Arrays, while others lists, so a bit confused.
Regards, Anthony.
Well I have some code but its not great to be honest from a different XML file that I parsed and just returned the data to screen. It used
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;
if that is any help?
Basically you're going to need to do the following:
- Open the XML file, most likely create an
InputStream
from it (file, network...) - Feed it to an XML parser
- Traverse the nodes and filter them according to user input
- Alternatively you might want to use XPath to filter the nodes
If you mention which XML library you are going to use, it would be easier to give you some code snippets.
Have a look at Java's possibilities to read XML files. I guess that would help you. http://java.sun.com/developer/codesamples/xml.html
For arrays vs lists, it just depends on how you're going to implement your sort operation. I usually prefer a List, and then I implement a Comparator to do the sort operation. From the data sample that you show in your question, my understanding is that you need to sort all the data by the timestamp field first, then return a range of values from that sorted list.
I am currently using JAXB for generating Java objects based upon an XML schema, then the XML-to-Java conversion is trivial. I'm not sure if you already have an approach for reading your XML file into objects. Do you have a schema definition for your XML files? If you do then JAXB will be easy to use.
精彩评论