dom解析xml

public class DOMParser {

 public static void main(String argv[]) {
  try {
   // 创建XML文档解析器
   DocumentBuilderFactory factory = DocumentBuilderFactory
     .newInstance();
   File xmlFile = new File("catalog.xml");
   // Create a DocumentBuilder
   DocumentBuilder builder = factory.newDocumentBuilder();
   // 解析XML文檔,這樣XML文檔就會加載到內存中,形成樹狀結構
   Document document = builder.parse(xmlFile);
   // 获取根节点
   Element rootElement = document.getDocumentElement();
   System.out.println("Root Element is: " + rootElement.getTagName());
   visitNode(null, rootElement);

  } catch (SAXException e) {
   System.out.println(e.getMessage());

  } catch (ParserConfigurationException e) {
   System.out.println(e.getMessage());

  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
 }

 public static void visitNode(Element previousNode, Element visitNode) {
  if (previousNode != null) {
   System.out.println("Element " + previousNode.getTagName()
     + " has element:");
  }
  System.out.println("Element Name: " + visitNode.getTagName());
  if (visitNode.hasAttributes()) {
   System.out.println("Element " + visitNode.getTagName()
     + " has attributes: ");
   NamedNodeMap attributes = visitNode.getAttributes();

   for (int j = 0; j < attributes.getLength(); j++) {
    Attr attribute = (Attr) (attributes.item(j));
    System.out.println("Attribute:" + attribute.getName()
      + " with value " + attribute.getValue());

   }
  }
  // Obtain a NodeList of nodes in an Element node.

  NodeList nodeList = visitNode.getChildNodes();
  for (int i = 0; i < nodeList.getLength(); i++) {
   Node node = nodeList.item(i);
   // Retrieve Element Nodes
   if (node.getNodeType() == Node.ELEMENT_NODE) {
    Element element = (Element) node;
    visitNode(visitNode, element);
   } else if (node.getNodeType() == Node.TEXT_NODE) {
    String str = node.getNodeValue().trim();
    if (str.length() > 0) {
     System.out.println("Element Text: " + str);

    }
   }
  }
 }
}

 

你可能感兴趣的:(xml,J#)