java - 如何使用 XPATH 到达特定的子节点?

标签 java xml xpath

我有一个 XML 文件,我正在尝试解析该文件以获取名为 Result 的标签.

使用 xpath name(//results/*)count(//results/*) ,我可以看到results有 10 个子节点,每个子节点名为 Result 。但如果我使用以下任何 xpath,则不会匹配:

  • //Result
  • /query/results/Result[1]
  • /query/results/Result

我所做的一切都不会返回任何 Result标签。但我知道有10个。我可以获取results通过/query/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>

最佳答案

元素结果属于默认命名空间“urn:yahoo:lcl”。查看问题的答案:

Getting elements with default namespace (no namespace prefix) using XPath

关于java - 如何使用 XPATH 到达特定的子节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47642254/

相关文章:

java - jar文件通过双击运行但不能通过cmd运行?

java - 如何使用 Selenium 和 Java 从动态下拉列表中选择自动建议

xml - 比较来自不同 XML 文件的元素值并附加到第一个 XML

在最大堆中分配字节 [] 时出现 Java OutOfMemoryError

java - 限制直接访问servlet页面

java - 创建Jar文件,复制类路径

java - 膨胀异常小部件回收 View

Java 配置到 XML 配置 - Spring Social

Android Button 背景无法识别/使用可绘制对象

java - 使用 xpath 获取 xml 文件的两个属性