使用SAX解析Amazon ItemSearch

时间:2012-01-30 19:46:32

标签: java android xml-parsing sax

我在使用SAX解析器解析Amazon产品搜索的XML输出时遇到问题。我试图只从XML中检索标题和链接。当我单步执行代码时,我从不输入任何监听器。

这是我到目前为止所做的:

 public class AmazonFeedParser 
 {
    public static List<AmazonItem> parse(final String url)
    {       
        final AmazonItem currentItem = new AmazonItem();
        final List<AmazonItem> returnItems = new ArrayList<AmazonItem>();

        final RootElement root = new RootElement("ItemSearchResponse");
        final Element items = root.getChild("Items");
        final Element item = items.getChild("Item");

        item.getChild("DetailPageURL").setEndTextElementListener(new EndTextElementListener()
        {
            public void end(final String body) {
                currentItem.setLink(body);
            }
        });

        final Element attributes = item.getChild("ItemAttributes");

        attributes.getChild("Title").setEndTextElementListener(new EndTextElementListener()
        {
            public void end(final String body) {
                currentItem.setTitle(body);
            }
        });

        item.setEndElementListener(new EndElementListener()
        {
            public void end() {
                returnItems.add(currentMessage.copy());
            }
        });

        try 
        {
            final InputStream stream = new URL(url).openConnection().getInputStream();

            Xml.parse(stream, Xml.Encoding.UTF_8, root.getContentHandler());
            stream.close();
        }
        catch (Exception e) 
        {
        }

        return returnItems;
    }
}

这是我试图解析的XML:

<ItemSearchResponse>
    <OperationRequest>
        <HTTPHeaders></HTTPHeaders>
        <RequestId>14BMDE9QG76M3Q9GG1XH</RequestId>
        <Arguments></Arguments>
        <RequestProcessingTime>0.34825611114502</RequestProcessingTime>
    </OperationRequest>
    <Items>
        <Request>
        </Request>
        <TotalResults>88693</TotalResults>
        <TotalPages>29570</TotalPages>
        <SearchResultsMap></SearchResultsMap>
    <Item>
        <ASIN>B00004XSC5</ASIN>
        <DetailPageURL>http://www.amazon.com/Taylor-1470-Digital-Cooking-Thermometer/dp/B00004XSC5%3FSubscriptionId%3DAKIAJI3JK6GP3HTDZNHQ%26tag%3Dfilt01-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB00004XSC5
        </DetailPageURL>
        <ItemLinks>
            <Description>Technical Details</Description>
            <URL>http://www.amazon.com/Taylor-1470-Digital-Cooking-Thermometer/dp/tech-data/B00004XSC5%3FSubscriptionId%3DAKIAJI3JK6GP3HTDZNHQ%26tag%3Dfilt01-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00004XSC5
            </URL>
        </ItemLinks>
        <ItemAttributes>
            <Manufacturer>Taylor Thermometers</Manufacturer>
            <ProductGroup>Kitchen</ProductGroup>
            <Title>Taylor 1470 Digital Cooking Thermometer/Timer</Title>
        </ItemAttributes>
    </Item>
 </Items>
</ItemSearchResponse>

编辑:如果它可以帮助任何我需要将Amazon命名空间添加到解析器代码中,例如:

    final RootElement root = new RootElement("http://webservices.amazon.com/AWSECommerceService/2009-03-31", "ItemSearchResponse");
    final Element items = root.getChild("http://webservices.amazon.com/AWSECommerceService/2009-03-31", "Items");
    final Element item = items.getChild("http://webservices.amazon.com/AWSECommerceService/2009-03-31", "Item");

    item.getChild("http://webservices.amazon.com/AWSECommerceService/2009-03-31", "DetailPageURL").setEndTextElementListener(new EndTextElementListener()
    {
        public void end(final String body) {
            currentMessage.setLink(body);
        }
    });

0 个答案:

没有答案