You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用DOM解析XML时如何处理缺失标签?

Java DOM解析XML时处理缺失标签的问题

你当前的代码逻辑是遍历<item>的子节点,仅当标签存在时才向对应List中添加内容,这会导致缺失标签的<item>在List中没有对应元素,最终两个List的长度可能不一致,也无法填充默认值。要实现标签缺失时写入指定字符串的需求,应该针对每个<item>先初始化默认值,再根据实际存在的标签覆盖,最后统一加入List。

原始XML内容

<item>
<title>number 1</title>
</item>

<item>
<title>number 2</title>
<enclosure url="https://www.google.com/"/>
</item>

原始Java代码片段

List<String> title = new ArrayList<>(); 
List<String> enclosure = new ArrayList<>();

NodeList itemNodeList = document.getElementsByTagName("item");
for (int i = 0; i < itemNodeList.getLength(); i++){
    if (itemNodeList.item(i).getNodeType() == Node.ELEMENT_NODE){
        Element itemElement = (Element) itemNodeList.item(i);

        NodeList childNodes = itemElement.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++){
            if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE){
                Element childElement = (Element) childNodes.item(j);
                
                switch (childElement.getNodeName()){
                    case "title":{ 
                        title.add(childElement.getTextContent());  
                        break;
                    }
                    case "enclosure":{ 
                        String info = childElement.getAttribute("url");
                        enclosure.add(info); 
                        break;
                    }
                }
            }
        }
    }
}

修改后的代码实现

List<String> titleList = new ArrayList<>(); 
List<String> enclosureList = new ArrayList<>();

NodeList itemNodeList = document.getElementsByTagName("item");
for (int i = 0; i < itemNodeList.getLength(); i++) {
    if (itemNodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
        Element itemElement = (Element) itemNodeList.item(i);
        
        // 为当前item初始化默认值
        String currentTitle = "No data";
        String currentEnclosure = "No data";

        NodeList childNodes = itemElement.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
            if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                Element childElement = (Element) childNodes.item(j);
                
                switch (childElement.getNodeName()) {
                    case "title":
                        currentTitle = childElement.getTextContent();
                        break;
                    case "enclosure":
                        currentEnclosure = childElement.getAttribute("url");
                        break;
                }
            }
        }
        // 每个item处理完后,将值加入List
        titleList.add(currentTitle);
        enclosureList.add(currentEnclosure);
    }
}

修改说明

  1. 初始化默认值:在每个<item>的处理循环内,先给currentTitle和currentEnclosure设置默认值"No data",确保无论标签是否存在都有值。
  2. 覆盖默认值:遍历子节点时,若找到对应标签,就用实际内容覆盖默认值。
  3. 统一添加到List:每个<item>处理完成后,再将当前的title和enclosure值加入List,保证List的长度和<item>数量一致,不会出现缺失项。

内容的提问来源于stack exchange,提问作者Илья Величко

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 14:20:15