简体   繁体   English

Java:从xml文件中读取LAST条目

[英]Java: Reading the LAST entry from an xml file

So I have a project where I need to pull an xml file that is updated every 5 minutes. 所以我有一个项目,我需要拉一个每5分钟更新一次的xml文件。 However I'm designing my program to pull this file every time it updates. 但是我正在设计我的程序以在每次更新时提取此文件。

The data structure of the xml file is like this... xml文件的数据结构是这样的......

<m:REPORT_DATA>
    <m:DATA_ITEM>ENC</m:DATA_ITEM>
    <m:RESOURCE_NAME>DLAP</m:RESOURCE_NAME>
    <m:OPR_DATE>2012-06-02</m:OPR_DATE>
    <m:INTERVAL_NUM>1</m:INTERVAL_NUM>
    <m:VALUE>16.77734</m:VALUE>
</m:REPORT_DATA>
<m:REPORT_DATA>
    <m:DATA_ITEM>ENC</m:DATA_ITEM>
    <m:RESOURCE_NAME>DLAP</m:RESOURCE_NAME>
    <m:DATE>2012-06-02</m:OPR_DATE>
    <m:INTERVAL_NUM>2</m:INTERVAL_NUM>
    <m:VALUE>16.77739</m:VALUE>
</m:REPORT_DATA>
....

Assuming that I pull it for the 200th time that day, how would I grab just the last value 假设我那天第200次拉它,我怎么才抓住最后一个值

"<m:VALUE>16.77739</m:VALUE>"

And get that value for my database? 为我的数据库获取该值?

I'm torn about using Sax, Xpath, or DOM. 我对使用Sax,Xpath或DOM感到很沮丧。 Some help would be amazing. 一些帮助会很棒。

If you had a root, lets say <m:REPORTS> , finding the last VALUE using XPath would be rather simple: 如果你有一个root,让我们说<m:REPORTS> ,使用XPath找到最后一个VALUE会相当简单:

    XPathFactory f = XPathFactory.newInstance() ;
    XPath x = f.newXPath() ;
    try {
        InputSource source = new InputSource(new FileInputStream("logfile.xml")) ;
        XPathExpression expr = x.compile("//REPORT_DATA[DATA_ITEM='ENC'][last()]/VALUE/text()") ;
        String s = expr.evaluate(source) ;
        System.out.println("Last value: " + s ) ;
    }
    catch(Throwable t) {
        System.err.println("Error: " + t) ;
    }

This isn't a well formed xml, You can use xpath to find last node, for example //REPORT_DATA[position() = last()] returns last REPORT_DATA node and for reading xml using xpath see How to read XML using XPath in Java 这不是格式良好的xml,您可以使用xpath查找最后一个节点,例如// REPORT_DATA [position()= last()]返回最后一个REPORT_DATA节点并使用xpath读取xml请参阅如何使用XPath读取XML Java的

//REPORT_DATA[last()]/DATA_ITEM[text()="ENC"] // REPORT_DATA [最后一个()] / DATA_ITEM [文本()= “ENC”]

and this returns node that it's DATA_ITEM equals to "ENC" 这会返回节点,它的DATA_ITEM等于“ENC”

or //REPORT_DATA[last()]/VALUE[text()="ENC"] 或// REPORT_DATA [last()] / VALUE [text()=“ENC”]

This is not an "XML File" in the sense that it is not well-formed, since it has no root element (or it has multiple root elements). 这不是一个“XML文件”,因为它没有格式良好,因为它没有根元素(或者它有多个根元素)。 As such it cannot be loaded directly by an XML library, so you cannot use DOM, XPath, or XSLT. 因此,它不能由XML库直接加载,因此您不能使用DOM,XPath或XSLT。

You are better off using some simple pattern matching to detect the start of each segment, find the last segment, and then load only that segment into a DOM for extraction. 最好使用一些简单的模式匹配来检测每个段的开始,找到最后一个段,然后仅将该段加载到DOM中进行提取。

Use SAX. 使用SAX。

With either xpath or DOM, you have to build a DOM which is slow and memory expensive, especially for 1 lookup. 使用xpath或DOM,您必须构建一个速度慢且内存昂贵的DOM,尤其是对于1次查找。

SAX is faster, but is going to require you to keep track of your place and state, which in your case should be easy. SAX速度更快,但需要您跟踪您的位置和状态,在您的情况下应该很容易。 Just look for your REPORT_DATA element, gather up its encapsulated data and if it is the last one (end document reached), you have your output. 只需查找您的REPORT_DATA元素,收集其封装的数据,如果它是最后一个(到达最终文档),您就有了输出。

    //filePath the path to the file you want to parse, tag  the tag of the node you want to search.    
public static String getLastNode(String filePath, String tag) throws             ParserConfigurationException, SAXException, IOException{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = docBuilder.parse(filePath);

    return doc.getElementsByTagName(tag).item(doc.getElementsByTagName(tag).getLength()-1).getTextContent();
//if you don't care about specific tag name just use :
//return doc.getLastChild().getTextContent;


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM