怎么实现java DOMParser 解析xml

发布时间:2020-07-13 13:49:12 作者:Leah
来源:亿速云 阅读:186

本篇文章为大家展示了怎么实现java DOMParser 解析xml,代码简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

import java.io.*;
import java.net.*;
import org.w3c.dom.*;
import org.w3c.dom.Node.*;
 
import oracle.xml.parser.v2.*;
 
/**
 * -----------------------------------------------------------------------------
 * Demonstrate how to use DOM.
 *
 * @version 1.0
 * @author  Jeffrey M. Hunter  (jhunter@idevelopment.info)
 * @author  http://www.idevelopment.info
 * -----------------------------------------------------------------------------
 */
 
public class DOMExample {
 
    /*
     * +---------------------------------------------+
     * | METHOD: main                                |
     * +---------------------------------------------+
     */
    static public void main(String[] argv) {
        try {
 
            if (argv.length != 1) {
                // must pass in the name of the XML file
                System.err.println("Usage: java DOMExample filename");
                System.exit(1);
            }
 
            // Get an instance of the parser
            DOMParser parser = new DOMParser();
 
            // Generate a URL from the filename
            URL url = createURL(argv[0]);
 
            // Set various parser options; validation on,
            // warnings shown, error stream set to stderr.
            parser.setErrorStream(System.err);
            parser.setValidationMode(true);
            parser.showWarnings(true);
            // parse the document
            parser.parse(url);
 
            // Obtain the document
            XMLDocument doc = parser.getDocument();
 
            // print document elements
            System.out.print("The elements are: ");
            printElements(doc);
 
            // print document elements attributes
            System.out.println("The attributes of each element are: ");
            printElementAttributes(doc);
 
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
 
    /*
     * +---------------------------------------------+
     * | METHOD: printElements                       |
     * +---------------------------------------------+
     */
    static void printElements(Document doc) {
 
        NodeList nodelist = doc.getElementsByTagName("*");
        Node     node;
 
        for (int i=0; i<nodelist.getLength(); i++) {
            node = nodelist.item(i);
            System.out.print(node.getNodeName() + " ");
        }
 
        System.out.println();
 
    }
 
    /*
     * +---------------------------------------------+
     * | METHOD: printElementAttributes              |
     * +---------------------------------------------+
     */
    static void printElementAttributes(Document doc) {
 
        NodeList      nodelist = doc.getElementsByTagName("*");
        Node          node;
        Element       element;
        NamedNodeMap  nnm = null;
 
        String attrname;
        String attrval;
        int    i, len;
 
        len = nodelist.getLength();
 
        for (int j=0; j < len; j++) {
            element = (Element)nodelist.item(j);
            System.out.println(element.getTagName() + ":");
            nnm = element.getAttributes();
        }
 
        if (nnm != null) {
            for (i=0; i<nnm.getLength(); i++) {
                node = nnm.item(i);
                attrname = node.getNodeName();
                attrval  = node.getNodeValue();
                System.out.println(" " + attrname + " = " + attrval);
            }
        }
 
        System.out.println();
 
    }
 
    /*
     * +---------------------------------------------+
     * | METHOD: createURL                           |
     * +---------------------------------------------+
     */
    static URL createURL(String filename) {
 
        URL url = null;
 
        try {
            url = new URL(filename);
        } catch (MalformedURLException ex) {
            try {
                File f = new File(filename);
                url = f.toURL();
            } catch (MalformedURLException e) {
                System.out.println("Cannot create URL for: " + filename);
                System.exit(0);
            }
        }
 
        return url;
 
    }
 
}

上述内容就是怎么实现java DOMParser 解析xml,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 如何使用Android实现XML解析技术
  2. java如何解析XML

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

domparser xml java

上一篇:MySQL安装教程

下一篇:如何彻底删除文件,让文件没办法恢复,保证数据安全?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》