首页前端开发HTMLDom解析xml小程序

Dom解析xml小程序

时间2024-01-25 13:16:18发布访客分类HTML浏览356
导读:收集整理的这篇文章主要介绍了html5教程-Dom解析xml小程序,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 [htML] package W...
收集整理的这篇文章主要介绍了html5教程-Dom解析xml小程序,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 [htML]
package WildCat.XMl.Dom;  
 
import java.io.File;  
import java.io.IOException;  
 
import javax.xml.parsers.DocumentBuilder;  
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserconfigurationException;  
 
import org.w3c.dom.Attr;  
import org.w3c.dom.COMment;  
import org.w3c.dom.Document;  
import org.w3c.dom.Element;  
import org.w3c.dom.NamedNodeMap;  
import org.w3c.dom.Node;  
import org.w3c.dom.nodelist;  
import org.xml.SAX.SAXException;  
 
public class testXml1_3 {  
 
    /** 
     * @param args 
     * @throws ParserConfigurationException  
     * @throws IOException  
     * @throws SAXException  
     */ 
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {  
        //step1.获得工厂 
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();  
        //setp2.获得解析器 
        DocumentBuilder db=dbf.newDocumentBuilder();  
        //steP3获得Document对象(根节点) 
        Document doc=db.parse(new File("test.xml"));  
        //获得根元素节点 
        Element root=doc.getDocumentElement();  
         
        parseElement(root);  
    }  
     
    public static void parseElement(Element ele) 
    {  
        //get the tag'sName 
        String tagName=ele.getNodeName();  
        //获得所有的孩子 
        NodeList children=ele.getChildNodes();  
        System.out.PRint(""+tagName);  
        NamedNodeMap map=ele.getAttributes();  
        if (null!=map) 
        {  
            for (int j=0; jmap.getLength(); j++) 
            {  
                //向下类型转换 
                Attr attr=(Attr)map.ITem(j);  
                //获得属性名 
                String attrName=attr.getNodeName();  
                //获得属性值 
                String attrValue=attr.getNodeValue();  
                System.out.print(" "+attrName+"=/""+attrValue+"/"");  
                 
            }  
             
        }  
        System.out.print("> ");  
         
        for (int i=0; ichildren.getLength(); i++) 
        {  
            Node node=children.item(i);  
            //get the node's tyPE 
            short nodeType=node.getNodeType();  
            if (Node.ELEMENT_NODE==nodeType) 
            {  
                //go on 递归 
                parseElement((Element)node);  
            }  
            else if (Node.TEXT_NODE==nodeType) 
            {  
                //if it is text  
                System.out.print(node.getNodeValue());  
                 
            }  
            else if (Node.COMMENT_NODE==nodeType) 
            {  
                System.out.print("!--");  
                Comment comment=(Comment)node;  
                //获得注释的内容 
                String data=comment.getData();  
                System.out.println(data+"--> ");  
            }  
             
        }  
        System.out.print("/"+tagName+"> ");  
         
     
         
    }  
     
     
 
}  

作者:superlele123 [html]
package WildCat.Xml.Dom;  
 
import java.io.File;  
import java.io.IOException;  
 
import javax.xml.parsers.DocumentBuilder;  
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;  
 
import org.w3c.dom.Attr;  
import org.w3c.dom.Comment;  
import org.w3c.dom.Document;  
import org.w3c.dom.Element;  
import org.w3c.dom.NamedNodeMap;  
import org.w3c.dom.Node;  
import org.w3c.dom.NodeList;  
import org.xml.sax.SAXException;  
 
public class testXml1_3 {  
 
    /** 
     * @param args 
     * @throws ParserConfigurationException  
     * @throws IOException  
     * @throws SAXException  
     */ 
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {  
        //step1.获得工厂 
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();  
        //setp2.获得解析器 
        DocumentBuilder db=dbf.newDocumentBuilder();  
        //step3获得Document对象(根节点) 
        Document doc=db.parse(new File("test.xml"));  
        //获得根元素节点 
        Element root=doc.getDocumentElement();  
         
        parseElement(root);  
    }  
     
    public static void parseElement(Element ele) 
    {  
        //get the tag'sName 
        String tagName=ele.getNodeName();  
        //获得所有的孩子 
        NodeList children=ele.getChildNodes();  
        System.out.print(""+tagName);  
        NamedNodeMap map=ele.getAttributes();  
        if (null!=map) 
        {  
            for (int j=0; jmap.getLength(); j++) 
            {  
                //向下类型转换 
                Attr attr=(Attr)map.item(j);  
                //获得属性名 
                String attrName=attr.getNodeName();  
                //获得属性值 
                String attrValue=attr.getNodeValue();  
                System.out.print(" "+attrName+"=/""+attrValue+"/"");  
                 
            }  
             
        }  
        System.out.print("> ");  
         
        for (int i=0; ichildren.getLength(); i++) 
        {  
            Node node=children.item(i);  
            //get the node's type 
            short nodeType=node.getNodeType();  
            if (Node.ELEMENT_NODE==nodeType) 
            {  
                //go on 递归 
                parseElement((Element)node);  
            }  
            else if (Node.TEXT_NODE==nodeType) 
            {  
                //if it is text  
                System.out.print(node.getNodeValue());  
                 
            }  
            else if (Node.COMMENT_NODE==nodeType) 
            {  
                System.out.print("!--");  
                Comment comment=(Comment)node;  
                //获得注释的内容 
                String data=comment.getData();  
                System.out.println(data+"--> ");  
            }  
             
        }  
        System.out.print("/"+tagName+"> ");  
         
     
         
    }  
     
     
 
}  

作者:superlele123

觉得可用,就经常来吧! 欢迎评论哦! html5教程,巧夺天工,精雕玉琢。小宝典献丑了!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

ClassdivDOMHTMLImportMappost-format-gallery

若转载请注明出处: Dom解析xml小程序
本文地址: https://pptw.com/jishu/586562.html
dfgallery 2.0 安装配置 poj2492并查集

游客 回复需填写必要信息