⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpledomparser.java

📁 这个是学习网络编程的好好文档! 里面有一些老师发给的学习jsp的课件!
💻 JAVA
字号:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.*;

public class SimpleDOMParser
{
  Document document;
  String eName;
  
  public SimpleDOMParser()
  {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try
    {
      DocumentBuilder builder = factory.newDocumentBuilder();
      document = builder.parse("student.xml");
    }
    catch(Exception sxe)
    {
    }
  }
  
  public void traverse(Node aNode)
  {
    switch(aNode.getNodeType())
    {
      case Node.DOCUMENT_NODE:
        System.out.println("Element "+aNode.getNodeName());
        processChildren(aNode.getChildNodes());
        break;
      case Node.ELEMENT_NODE:
        eName = aNode.getNodeName();
        System.out.println("Element "+eName);
        NamedNodeMap attrList = aNode.getAttributes();
        for(int i=0; i<attrList.getLength(); i++)
        {
          Attr attr = (Attr)attrList.item(i);
          String aName = attr.getNodeName();
          String aValue = attr.getNodeValue();
          System.out.println(aName+":"+aValue); 
        }
        processChildren(aNode.getChildNodes());
        break;
      case Node.CDATA_SECTION_NODE:
      case Node.TEXT_NODE:
        if(!aNode.getNodeValue().trim().equals(""))
        {
          System.out.println("eName " + eName);
          System.out.println("Text  " + aNode.getNodeValue());
        }
        break;
    }
  }
  
  private void processChildren(NodeList nList)
  {
    if(nList.getLength() != 0)
    {
      for(int i=0; i<nList.getLength(); i++)
        traverse(nList.item(i));
    }
  }
  
  public static void main(String[] args)
  {
    SimpleDOMParser sdp = new SimpleDOMParser();
    sdp.traverse(sdp.document);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -