java如何使用XPATH访问特定的子节点?
我有一个XML文件,我正试图解析它以获得名为Result
的标记
使用xpath name(//results/*)
和count(//results/*)
,我可以看到results
有10个子节点,每个节点都被命名为Result
。但如果我使用以下任何一种XPath,则没有匹配项:
//Result
/query/results/Result[1]
/query/results/Result
我所做的一切都不是返回任何Result
标记。但我知道有10个。我可以通过/query/results
获得results
,因此我知道我正在连接到此文件。如何生成xpath以到达每个Result
XML结构(缩写):
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
yahoo:count="10" yahoo:created="2013-07-17T15:15:52Z" yahoo:lang="en-US">
<diagnostics>
</diagnostics>
<results>
<Result id="11762112" xmlns="urn:yahoo:lcl">
<Title>California Rollin' Sushi Bar</Title>
<Address>274 Goodman St N</Address>
<City>Rochester</City>
<State>NY</State>
<Phone>(585) 271-8990</Phone>
<Latitude>43.158685</Latitude>
<Longitude>-77.585197</Longitude>
<Rating>
<AverageRating>3</AverageRating>
<TotalRatings>10</TotalRatings>
<TotalReviews>10</TotalReviews>
<LastReviewDate>1341259499</LastReviewDate>
<LastReviewIntro> I went with my two girls last week and was told it would be around 1-1/2 wait so we had our name put on list hoping we would get seated sooner. It was busy because the groupons were expiring (which they knew ahead of time, so one would think they would be prepared). Well needless to say after 1-1/2 we still were not seated and there were several tables available. I asked how much longer and the hostess said another hour. I told her we have already been waiting for 1 hour she smirked and said sorry. People were coming in after us and being seated which made one of my girls ask the question why do they get a table before us. I went back in and said since my kids are tired is there anyway I could order take out and use my groupon since I am here and of course the hostess replied no its for dine in only. Ugh we left and ate elsewhere. I did call to speak with the manager who really was more concerned about telling me how proud she was of her staff and really didnt seem to care about our awful experience. Too bad because they lost several customers that night. What ever happend to customer service?</LastReviewIntro>
</Rating>
<Distance>1.57</Distance>
<Url>http://local.yahoo.com/info-11762112-california-rollin-sushi-bar-rochester</Url>
<ClickUrl>http://local.yahoo.com/info-11762112-california-rollin-sushi-bar-rochester</ClickUrl>
<MapUrl>http://local.yahoo.com/info-11762112-california-rollin-sushi-bar-rochester?viewtype=map</MapUrl>
<BusinessUrl>http://californiarollin.com/</BusinessUrl>
<BusinessClickUrl>http://californiarollin.com/</BusinessClickUrl>
<Categories>
<Category id="96926236">Restaurants</Category>
<Category id="96926205">Sushi Restaurants</Category>
</Categories>
</Result>
</results>
</query>
# 1 楼答案
元素结果属于默认名称空间“urn:yahoo:lcl”。见对问题的答复:
Getting elements with default namespace (no namespace prefix) using XPath
# 2 楼答案
我尝试了您的xml,并关闭了
results
标记,我认为这是您复制和粘贴xml的一部分。除此之外,一切正常。我添加了更多的Result
标记来测试它,并得到了预期的结果。请查看:输出:
请注意
California 2
以指示它是第二个Result
条目我也试过
//Result
和/query/results/Result
您手头的xml有什么问题吗?你能用我贴的代码试试吗?干杯