如何使用XMLreader从XML文件中检索一个特定的数据?

时间:2016-09-19 18:22:59

标签: java xml xml-parsing processing

我必须解析的XML数据的一个真实示例以及文件的配置方式。这就是文件呈现给我的方式。

<?xml version="1.0"?>
 <session>
  <values>
   <value id="FILE_CREATE_DATE">
    <timestamp>2012-04-16T21:33:31Z</timestamp>
   </value>
   <value id="LAST_ACCESSED">
    <timestamp>2012-09-17T17:15:23Z</timestamp>
   </value>
   <value id="VERSION_TIMESTAMP">
    <timestamp>2012-04-16T21:33:31Z</timestamp>
   </value>
 </values>
</session>

我需要进入此文件并检索FILE_CREATE_DATE数据。

到目前为止我的代码:

    File xmlFile = new File(XMLFileData[i].getPath());
    FileInputStream myXMLStream = new FileInputStream(xmlFile);

    XMLInputFactory XMLFactory = XMLInputFactory.newInstance();
    XMLStreamReader XMLReader =  XMLFactory.createXMLStreamReader(myXMLStream);
    while(XMLReader.hasNext())
    {
       if (XMLReader.getEventType() == XMLStreamReader.START_ELEMENT)
       {        
          String XMLTag = XMLReader.getLocalName();
          if(XMLReader.hasText())
          {
             System.out.println(XMLReader.getText());
             break;
          }                            
       }    
       XMLReader.next();
    }

'getLocalName()'函数返回'Sessions'然后'value'然后返回'values'但是永远不会返回元素的实际名称。我需要测试一下,看看我是否在正确的元素,然后从该元素中检索数据...

4 个答案:

答案 0 :(得分:0)

您的ID不是元素 - 它的元素属性。

您应该读取您的value节点的属性,请参阅getAttributeValue方法的javadoc:

http://docs.oracle.com/javase/6/docs/api/javax/xml/stream/XMLStreamReader.html#getAttributeValue(java.lang.String,%20java.lang.String)

  

返回属性的规范化属性值   namespace和localName如果namespaceURI为null,则命名空间为   未检查是否相等

所以它会是:

String XMLTag = XMLReader.getLocalName();
if(XMLTag.equals("value")) {
   String idValue = XMLReader.getAttributeValue(null, "id");
   //here idValue will be equal to FILE_CREATE_DATE, LAST_ACCESSED or VERSION_TIMESTAMP
}

答案 1 :(得分:0)

我使用Jsoup作为解析HTML的库。但它也可以用于xml。您首先必须将XML文件加载到Document对象中,然后只需调用

doc.getElementById("FILE_CREATE_DATE");

这将返回一个Element对象,该对象将时间戳作为子对象。这是指向图书馆的链接:https://jsoup.org/

这是我的第一个StackOverflow答案,请告诉我它是否有帮助!

答案 2 :(得分:0)

尝试类似

的内容
if(XMLReader.getAttributeValue(0).equalIgnorecase("FILE_CREATE_DATE"))

getAttributeValue:返回属性的给定索引的值。为了

<value id="FILE_CREATE_DATE">

id是第一个属性。所以XMLReader.getAttributeValue(0)

但在调用之前,您必须验证元素是否具有第一个属性。因为所有标签都没有至少1个属性。

在jsoup中你可以像这样查询

public static void main(String[] args) {

Document doc;
try {
    doc = Jsoup.connect("http://www.dropbox.com/public/xml/yourfile.xml").userAgent("Mozilla").get();
    //<value id="FILE_CREATE_DATE">
    Elements links = doc.select("value[id=FILE_CREATE_DATE]");
    for (Element link : links) {            
        if(link.attr("id").contains("FILE_CREATE_DATE"))//find the link with some texts
        {
            System.out.println("here is the element you need");
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

}

答案 3 :(得分:0)

Restoring NuGet packages...
To prevent NuGet from restoring packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages during build.'
1>------ Rebuild All started: Project: MoneyFox.Shared, Configuration: Debug Any CPU ------
1>C:\Users\nino\Documents\GitHub\MoneyFox.Windows\Src\MoneyFox.Shared\MoneyFox.Shared.csproj(333,3): warning MSB4011: "C:\Users\nino\Documents\GitHub\MoneyFox.Windows\Src\packages\Fody.1.29.4\build\portable-net+sl+win+wpa+wp\Fody.targets" cannot be imported again. It was already imported at "C:\Users\nino\Documents\GitHub\MoneyFox.Windows\Src\MoneyFox.Shared\MoneyFox.Shared.csproj (325,3)". This is most likely a build authoring error. This subsequent import will be ignored. 
1>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): error MSB3270: There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\\mscorlib.dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

所以这段代码是我对从XML数据文件中恢复特定数据这一主题的所有痛苦的最终结果。我要感谢所有帮助我解答答案的人 - 无论是我想要的是什么,他们都让我思考,这导致了解决方案......