2

I have a XML file that I'm trying to parse to get tags named Result.

With the xpath name(//results/*) and count(//results/*), I can see that results has 10 child nodes and that each is named Result. But there is no match if I use any of the following xpaths:

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

Nothing I am doing is returning any Result tag. But I know that there are 10. I can obtain results via /query/results, so I know I'm connecting to this file. How do I generate a xpath to reach each Result?

The XML Structure (shortened):

<?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>
brienna
  • 1,415
  • 1
  • 18
  • 45

2 Answers2

0

I've tried your xml and had to close the results tag, which I assume was part of you copying and pasting just a bit of the xml. Apart from that, everything worked fine. I've added more Result tags to test it and got what was expected. Check it out:

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());
        }
    }
}

Output:

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

Notice the California 2 to indicate that it was the second Result entry.

I've also tried //Result and /query/results/Result.

Is there anything wrong with the xml that you have in hand? Could you try it with the code I posted? Cheers!

wleao
  • 2,316
  • 1
  • 18
  • 17
  • Thanks! Nothing's wrong with the XML except for forgetting to close the `results` tag. I also commented on the above answer – brienna Dec 04 '17 at 23:16
0

Element Result belongs to the default namespace "urn:yahoo:lcl". See answer to the question:

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

Pang
  • 9,564
  • 146
  • 81
  • 122
mg_kedzie
  • 337
  • 1
  • 9