首页 > 解决方案 > Java SAX Parser 子元素与父元素具有相同标签时

问题描述

我正在尝试使用属于 div 标签的项目列表从网站爬取数据。然后在该单个项目中,也使用 div 标签制作了两个单独的部分。一张有图片,一张有文字和描述。在 startElement 中,我可以用 Attribute 来识别它们,但我不能以 endElement 结尾。如何解析具有相同标签的项目?

我要抓取的项目示例:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <div class="o-ResultCard__m-MediaBlock m-MediaBlock">
        <div class="m-MediaBlock__m-TextWrap">
            <h3 class="m-MediaBlock__a-Headline">
                <a href="abc.com"><span class="m-MediaBlock__a-HeadlineText">Air Fryer Chicken Wings</span></a>
            </h3>
            <div class="parbase recipeInfo time">
                <section class="o-RecipeInfo__o-Time">
                    <dl>
                        <dt class="o-RecipeInfo__a-Headline a-Headline">Total Time: 40 minutes</dt>
                    </dl>
                </section>
            </div>
        </div>
        <div class="m-MediaBlock__m-MediaWrap">
            <a href="abc.com" class="" title="Air Fryer Chicken Wings">
                <img src="https://dinnerthendessert.com/wp-content/uploads/2019/01/Fried-Chicken-2.jpg" class="m-MediaBlock__a-Image" alt="Air Fryer Chicken Wings">
            </a>
        </div>
    </div>
</body>

我的处理程序:

private String currentTag;
private FoodDAO dao;
private FoodsDTO dto;
private String itemIdentify = "o-ResultCard__m-MediaBlock m-MediaBlock";
private String itemMedia = "m-MediaBlock__m-MediaWrap";
private String itemText = "m-MediaBlock__m-TextWrap";
private boolean foundItem;

public FoodHandler() {
    dao = new FoodDAO();
    foundItem = false;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    String attrVal = attributes.getValue(0);

    if (qName.equals("div") && attrVal.equals(itemIdentify)) {
        dto = new FoodsDTO();
        foundItem = true;
    }
    currentTag = qName;
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (qName.endsWith("div")) {
        foundItem = false;
        try {
            dao.manageCrawl(dto);
        } catch (Exception e) {
            Logger.getLogger(NewsHandler.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    currentTag = "";
}

标签: javaxmlsaxsaxparser

解决方案


停止堆栈中的属性。

更具体地说,将属性的副本Deque存储在 a 中:

private Deque<Attributes> attributesStack = new ArrayDeque<>();

@Override
public void startDocument() throws SAXException {
    // Clear the stack at start of parsing, in case this handler is
    // re-used for multiple parsing operations, and previous parse failed.
    attributesStack.clear();
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    attributesStack.push(new AttributesImpl(attributes)); // Attributes must be copied
    
    // code here
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    Attributes attributes = attributesStack.pop();
    
    // code here
}

推荐阅读