我是靠谱客的博主 文静自行车,最近开发中收集的这篇文章主要介绍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的实例详解的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是文静自行车为你收集整理的java DOMParser 解析xml的实例详解的全部内容,希望文章能够帮你解决java DOMParser 解析xml的实例详解所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部