wmlprocessor.java~4~

来自「一个很好xml学习样例」· JAVA~4~ 代码 · 共 34 行

JAVA~4~
34
字号
package xmltest;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class WMLProcessor {
  public static void main(String[] args) {
    File xmlFile = new File("test.xml");
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
      DocumentBuilder db = dbf.newDocumentBuilder();
      doc = db.parse(xmlFile);
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    catch (SAXException ex) {
      ex.printStackTrace();
    }
    catch (ParserConfigurationException ex) {
      ex.printStackTrace();
    }
    Element root = doc.getDocumentElement();
    System.out.println("The root element is: " + root.getNodeName());
    NodeList children = root.getChildNodes();
    System.out.println("There are " + children.getLength() + " nodes in the doc");
    for (Node child = root.getFirstChild() ; child != null; child = child.getNextSibling()){
      System.out.println(child.getNodeName() + " = " + child.getNodeValue());
    }
  }
}

⌨️ 快捷键说明

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