DOM中node和element區別

 1.元素(Element)和結點(Node)的區別,元素是一個小範圍的定義,必須是含有完整信息的結點纔是一個元素,例如<div>...</div>。但是一個結點不一定是一個元素,而一個元素一定是一個結點。
什麼是node:

 

NODE是相對TREE這種數據結構而言的。TREE就是由NODE組成。這個部分你可以參考離散數學的樹圖。

什麼是element

ELEMENT則是XML裏的概念,<xxx>就是元素,是XML中的數據的組成部分之一。

素(Element)和結點(Node)的區別,元素是一個小範圍的定義,必須是含有完整信息的結點纔是一個元素,例如<div>...</div>。但是一個結點不一定是一個元素,而一個元素一定是一個結點。

<a>

  <b> </b>

  <b> </b>

<a>

DOM將文檔中的所有都看作節點 node>element

1DOM在解析文檔的時候按整個文檔的結構生成一棵樹,全部保存在內存

優點就是整個文檔都一直在內存中,我們可以隨時訪問任何節點,並且對樹的遍歷也是比較熟悉的操作;缺點則是耗內存,並且必須等到所有的文檔都讀入內存才能進行處理。
2一個需要注意的地方就是,XML文檔兩個標籤之間的空白也是這棵樹的一個節點(Text節點)。 <a> <b></b> <a> a有三個節點

Element root = doc.getDocumentElement();:root是什麼????

NodeList list = root.getChildNodes();             root 到底是節點還是元素我不清楚?????


node有幾個子類型:

    Element,

      Text,

    Attribute,

  RootElement,

    Comment,

    Namespace等
Element是可以有屬性和子節點的node。

Element是從Node繼承的

//轉換

if (node.getNodeType() == Element.ELEMENT_NODE)

{
     Element e = (Element) node; 

}

 

 ?元素有孩子嗎

elemen et 性質

1 e.getAttributes()

2 e.getChildNodes()

3 e.getTagName()

 

Element root = doc.getDocumentElement();:root是什麼????

NodeList list = root.getChildNodes();             root 到底是節點還是元素我不清楚???

······················································

public void domParse(String fileName) throws Exception {
  DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = f.newDocumentBuilder();//builder
  Document docment = db.parse(new File(fileName));//parese

  Element el = docment.getDocumentElement();//root
  domRead(el);
  
 }

 public void domRead(Element currentNode) {
  if ("struts-config".equals(currentNode.getNodeName())) {
   config = new StrutsConfig();
  }
        
  NodeList list = currentNode.getChildNodes();
  for (int i = 0; i < list.getLength(); i++) {
   Node node = list.item(i);
   if (node.getNodeType() == Element.ELEMENT_NODE) {
    Element e = (Element) node;//????
              
    if ("form-beans".equals(e.getTagName())) {
     formBeans = new ArrayList<FormBeanConfig>();
     domRead(e);
    }
    if ("form-bean".equals(e.getTagName())) {
     FormBeanConfig fc = new FormBeanConfig();
     NamedNodeMap attrs = e.getAttributes();
     
     for (int j = 0; j < attrs.getLength(); j++) {
      Attr attr = (Attr) attrs.item(j);
      if ("name".equals(attr.getName())) {
       fc.setName(attr.getValue());
      } else {
       fc.setType(attr.getValue());
      }
     }
     formBeans.add(fc);
    }
    if ("action-mapping".equals(e.getTagName())) {
     actions = new ArrayList<ActionConfig>();
     domRead(e);
    }
    if ("action".equals(e.getTagName())) {
     ActionConfig ac = new ActionConfig();
     NamedNodeMap attrs = e.getAttributes();
     for (int k = 0; k < attrs.getLength(); k++) {
      Attr attr = (Attr) attrs.item(k);
      if ("path".equals(attr.getName())) {
       ac.setPath(attr.getValue());
      } else if ("type".equals(attr.getName())) {
       ac.setType(attr.getValue());
      } else {
       ac.setName(attr.getValue());
      }
     }

     actions.add(ac);
    }
   }
  }
 }

tree-------xml 文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章