• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

java DOMParser xml的

武飞扬头像
PHP中文网
帮助38

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;
 
    }
 
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfgfgj
系列文章
更多 icon
同类精品
更多 icon
继续加载