2013-10-30 43 views
0
<time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p24h-n7-1</layout-key> 
     <start-valid-time>2013-10-30T08:00:00-07:00</start-valid-time> 
     <end-valid-time>2013-10-30T20:00:00-07:00</end-valid-time> 
     <start-valid-time>2013-10-31T08:00:00-07:00</start-valid-time> 
     <end-valid-time>2013-10-31T20:00:00-07:00</end-valid-time> 
     <start-valid-time>2013-11-01T08:00:00-07:00</start-valid-time> 
     <end-valid-time>2013-11-01T20:00:00-07:00</end-valid-time> 
     <start-valid-time>2013-11-02T08:00:00-07:00</start-valid-time> 
     <end-valid-time>2013-11-02T20:00:00-07:00</end-valid-time> 
     <start-valid-time>2013-11-03T07:00:00-08:00</start-valid-time> 
     <end-valid-time>2013-11-03T19:00:00-08:00</end-valid-time> 
     <start-valid-time>2013-11-04T07:00:00-08:00</start-valid-time> 
     <end-valid-time>2013-11-04T19:00:00-08:00</end-valid-time> 
     <start-valid-time>2013-11-05T07:00:00-08:00</start-valid-time> 
     <end-valid-time>2013-11-05T19:00:00-08:00</end-valid-time> 
    </time-layout> 
    <time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p24h-n6-2</layout-key> 
     <start-valid-time>2013-10-30T20:00:00-07:00</start-valid-time> 
     <end-valid-time>2013-10-31T09:00:00-07:00</end-valid-time> 
     <start-valid-time>2013-10-31T20:00:00-07:00</start-valid-time> 
     <end-valid-time>2013-11-01T09:00:00-07:00</end-valid-time> 
     <start-valid-time>2013-11-01T20:00:00-07:00</start-valid-time> 
     <end-valid-time>2013-11-02T09:00:00-07:00</end-valid-time> 
     <start-valid-time>2013-11-02T19:00:00-07:00</start-valid-time> 
     <end-valid-time>2013-11-03T08:00:00-08:00</end-valid-time> 
     <start-valid-time>2013-11-03T19:00:00-08:00</start-valid-time> 
     <end-valid-time>2013-11-04T08:00:00-08:00</end-valid-time> 
     <start-valid-time>2013-11-04T19:00:00-08:00</start-valid-time> 
     <end-valid-time>2013-11-05T08:00:00-08:00</end-valid-time> 
    </time-layout> 
    <time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p3h-n35-3</layout-key> 
     <start-valid-time>2013-10-30T11:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-30T14:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-30T17:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-30T20:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-30T23:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-31T02:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-31T05:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-31T08:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-31T11:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-31T14:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-31T17:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-31T20:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-10-31T23:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-01T02:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-01T05:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-01T08:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-01T11:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-01T14:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-01T17:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-01T23:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-02T05:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-02T11:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-02T17:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-02T23:00:00-07:00</start-valid-time> 
     <start-valid-time>2013-11-03T04:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-03T10:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-03T16:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-03T22:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-04T04:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-04T10:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-04T16:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-04T22:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-05T04:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-05T10:00:00-08:00</start-valid-time> 
     <start-valid-time>2013-11-05T16:00:00-08:00</start-valid-time> 
    </time-layout> 

當前我正在使用Java中的XPath解析XML。一切都很好,直到我看到這三個街區。我遇到的困難是我遇到了三個<time-layout time-coordinate="local" summarization="none">,每個都有不同類型的信息。我目前的「黑客」是將local重命名爲local1local2local3,所以我通過/dwml/data/time-layout[@time-coordinate='local3']/start-valid-time/text()解析XPath。這是一種黑客攻擊,但是由於我將很快從該XML所在的站點直接讀取XML,所以我不想使用這種方式,或者我可能無法使用該黑客。由於這裏有三個名稱相似的元素,我如何僅使用XPath解析第三個元素,而不重命名其中一個屬性?使用XPath解析相同的XML元素

謝謝!

P.S. <layout-key>值每次都有所不同,所以我無法將其硬編碼到解析器中。

回答

1

使用謂語:

//time-layout[3] 
+0

太棒了!非常感謝!這有很大幫助。 – theGreenCabbage

+0

有趣的是,在XPath中,第一個元素不是從0開始的。 – theGreenCabbage

+0

看看w3schools上的XPath示例 – resigned

1

那麼你可以在你的xpath謂詞中檢查<end-valid-time>的存在,如果它不在那裏,你正在看第3個元素。

//time-layout[not(./end-valid-time)] 

這也是一個黑客,但至少它都在xpath中。

+0

對不起 - 這將是完整的XPath字符串?我試過'/ dwml/data/time-layout [不是(./ end-valid-time)]/text()'但是沒有工作。 – theGreenCabbage

+0

'NodeList hourlyResult =(NodeList)hourly.evaluate(doc,XPathConstants.NODESET);'和'for(int i = 0; i theGreenCabbage