我有一个XML文件,我正试图解析它以获得名为Result
的标记。
使用xpathname(//results/*)
和count(//results/*)
,我可以看到results
有10个子节点,每个节点都被命名为Result
。但如果我使用以下任何一种XPath,就没有匹配项:
//结果
/查询/结果/结果[1]
/查询/结果/结果
我所做的一切都不是返回任何Result
标记。但是我知道有10个。我可以通过/查询/结果
获得结果
,所以我知道我正在连接到这个文件。我如何生成一个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>
我尝试了你的xml,不得不关闭results
标记,我认为这是你复制和粘贴xml的一部分。除此之外,一切正常。我添加了更多的Result
标签来测试它,得到了预期的结果。过来看:
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TestXpath {
public static void main(String[] args) throws IOException, SAXException, IOException, ParserConfigurationException, XPathExpressionException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document parse = builder.parse(new FileInputStream("test.xml"));
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/query/results/Result[2]");
NodeList nl = (NodeList) expr.evaluate(parse, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
System.out.println(item.getNodeName());
System.out.println(item.getTextContent());
}
}
}
输出:
Result
California 2 Rollin' Sushi Bar
274 Goodman St N
Rochester
NY
(585) 271-8990
43.158685
-77.585197
3
10
10
1341259499
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?
1.57
http://local.yahoo.com/info-11762112-california-rollin-sushi-bar-rochester
http://local.yahoo.com/info-11762112-california-rollin-sushi-bar-rochester
http://local.yahoo.com/info-11762112-california-rollin-sushi-bar-rochester?viewtype=map
http://californiarollin.com/
http://californiarollin.com/
Restaurants
Sushi Restaurants
请注意California 2
指示它是第二个Result
条目。
我还尝试了//Result
和/query/results/Result
。
您手头的xml有什么问题吗?你能用我发的代码试试吗?干杯
元素结果属于默认名称空间“urn:yahoo:lcl”。见问题的答案:
使用XPath获取具有默认命名空间(无命名空间前缀)的元素
如何获取此html片段中a的href值? 我需要根据I标记中的类获取它 我试过了,但没有结果
我试图找到一个有两个子节点的web元素: 我尝试了来获取第一个元素,但对我来说不起作用。
问题内容: 假设我们有一个HTML表格,基本上是这样的: 我想选择仅包含8个元素的单元格,即,第2行的第2个单元格和第3行的第3个单元格。 这是我尝试过的:。它给了我所有包含8的单元格。因此,我也得到了不必要的值28和18。 我该如何解决? 编辑: 这是一个示例表,如果您想尝试您的xpath。使用左侧的日历-https : //sfbay.craigslist.org/sfc/ 问题答案: 请小心
我有一个SVG文档,其中包含类似于以下内容的节点: 我想做的只是选择
我想使用Xpath提取父节点和子节点之间的vale。如何做到这一点? 如何将文本值输出为“要提取的值”?它不应该是“要提取密钥的值”“关键”应该被忽略。
如果我有 如果我想选择包含td的a,我该怎么做? 我测试过: 但是我要获得td节点,我要获得a节点。如何使用XPath实现这一点?