Java 怎么从给定的XML元素中获取指定名称下的属性值

2019年7月21日19:42:24Java 怎么从给定的XML元素中获取指定名称下的属性值已关闭评论 459 1261字阅读4分12秒

1.简介

Java 怎么从给定的XML元素中获取指定名称下的属性值?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

   Copyright (C) 2016 HermeneutiX.org

   This file is part of SciToS.

   SciToS is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   SciToS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with SciToS. If not, see <http:
 */
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main{
    
    public static int getIntAttribute(final Element node,
            final String attributeName, final int defaultValue) {
        final String stringValue = DomUtil.getNullableAttribute(node,
                attributeName);
        if (stringValue != null) {
            try {
                return Integer.parseInt(stringValue);
            } catch (final NumberFormatException expected) {
                
            }
        }
        return defaultValue;
    }
    
    public static String getNullableAttribute(final Element node,
            final String attributeName) {
        if (node.hasAttribute(attributeName)) {
            return node.getAttribute(attributeName);
        }
        return null;
    }
}

 
继续阅读
编程十万个怎么办