大家说说怎么解析XML?(面试题)

解决方案 »

  1.   

    js中解析
    <html>
    <head>
    <script type="text/javascript"
    for="window" event="onload">
    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
    xmlDoc.async="false"
    xmlDoc.load("xml_note.xml")
    nodes=xmlDoc.documentElement.childNodes
    to.innerText=    nodes.item(0).text
    from.innerText=  nodes.item(1).text
    header.innerText=nodes.item(2).text
    body.innerText=  nodes.item(3).text
    </script>
    <title>HTML using XML data</title>
    </head>
    <body bgcolor="yellow">
    <h1>W3Schools.com Internal Note</h1>
    <b>To: </b>
    <span id="to"> </span>
    <br />
    <b>From: </b>
    <span id="from"></span>
    <hr>
    <b><span id="header"></span></b>
    <hr>
    <span id="body"></span>
    </body>
    </html>
      

  2.   

    java中解析
      
       
    import java.io.File;    
    import java.io.IOException;    
    import javax.xml.parsers.DocumentBuilder;    
    import javax.xml.parsers.DocumentBuilderFactory;    
    import javax.xml.parsers.ParserConfigurationException;    
    //下面主要是org.xml.sax包的类    
    import org.w3c.dom.*;    
    import org.xml.sax.SAXException;    
       
    public class DOMPrinter{    
        //打印指定节点(节点名:节点值)    
        public static void printNodeInfo(Node node){    
            System.out.println(node.getNodeName()+" : "+node.getNodeValue());    
        }    
        //递归打印全部节点    
        public static void printNode(Node node){    
            short nodeType=node.getNodeType();    
            //判断节点类型    
            switch(nodeType){    
                //前缀节点    
                case Node.PROCESSING_INSTRUCTION_NODE:    
                    System.out.println("-----------PI start-----------");    
                    printNodeInfo(node);    
                    System.out.println("-----------PI end-----------");    
                    break;    
                //元素节点    
                case Node.ELEMENT_NODE:    
                    System.out.println("-----------Element start-----------");    
                    printNodeInfo(node);    
                    System.out.println("-----------Element end-----------");    
                    //打印节点属性值    
                    NamedNodeMap attrs=node.getAttributes();    
                    int attrNum=attrs.getLength();    
                    for(int i=0;i<attrNum;i++){
                        Node attr=attrs.item(i);    
                        System.out.println("-----------Attribute start-----------");    
                        printNodeInfo(attr);    
                        System.out.println("-----------Attribute end-----------");    
                    }    
                    break;    
                //文本节点    
                case Node.TEXT_NODE:    
                    System.out.println("-----------Text start-----------");    
                    printNodeInfo(node);    
                    System.out.println("-----------Text end-----------");    
                    break;    
                default:    
                    break;    
            }    
            //递归打印节点信息    
            Node child=node.getFirstChild();    
            while(child!=null){    
                printNode(child);    
                child=child.getNextSibling();    
            }    
        }    
            
        public static void main(String[] args){    
            //DOM解析器的工厂实例    
            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();    
            try{    
                //从DOM工厂获得DOM解析器    
                DocumentBuilder db=dbf.newDocumentBuilder();    
                //解析XML文档的输入流,得到一个Document    
                Document doc=db.parse(new File("students.xml"));    
                //打印节点    
                printNode(doc);    
            }    
            catch (ParserConfigurationException e){    
                e.printStackTrace();    
            }    
            catch (SAXException e){    
                e.printStackTrace();    
            }    
            catch (IOException e){    
                e.printStackTrace();    
            }    
        }    
    }