首頁  >  文章  >  後端開發  >  詳解XML中Node和Element區別的範例程式碼

詳解XML中Node和Element區別的範例程式碼

黄舟
黄舟原創
2017-03-23 16:25:581613瀏覽

                                                  一個小範圍的定義,必須是含有完整資訊的結點才是一個元素,例如e388a4556c0f65e1904146cc1a846bee... 94b3e26ee717c64999d7867364b1b4a3。但是一個結點不一定是一個元素,而一個元素一定是個結點。
什麼是node:
 
NODE是相對TREE這種資料結構而言的。 TREE就是由NODE組成。這個部分你可以參考離散數學的樹圖。
 
什麼是element
 
ELEMENT則是
XML裡的概念,d55b48f42965904505cada53ebca2b59就是元素,是XML中的資料的組成部分之一。  
素(Element)和結點(Node)的區別,元素是一個小範圍的定義,必須是含有完整資訊的結點才是一個元素,例如e388a4556c0f65e1904146cc1a846bee...6fb279ad3fd4344cbdd93aac6ad173ac。但是一個結點不一定是一個元素,而一個元素一定是個結點。

 
<a>
 
  <b> </b>
 
  <b> </b>
 
<a>

 

DOM將文件中的所有都看作節點node>element
 
1DOM在解析文件的時候按整個文件的結構生成一棵樹,全部保存在內存
 
優點就是整個文件都一直在記憶體中,我們可以隨時存取任何節點,並且對樹的遍歷也是比較熟悉的操作;缺點則是耗內存,並且必須等到所有的文檔都讀入記憶體才能進行處理。
2一個要注意的地方就是,XML文件兩個標籤之間的空白也是這棵樹的一個節點(Text節點)。 3499910bf9dac5ae3c52d5ede7383485 a4b561c25d9afb9ac8dc4d70affff4190d36329ec37a2cc24d42c7229b69747a 3499910bf9dac5ae3c52d5ede7383485 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);
    }
   }
  }
 }

以上是詳解XML中Node和Element區別的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn