Home  >  Article  >  Backend Development  >  Sample code that explains the difference between Node and Element in XML

Sample code that explains the difference between Node and Element in XML

黄舟
黄舟Original
2017-03-23 16:25:581534browse

                                                                                                                                                                                                                                                                                       94b3e26ee717c64999d7867364b1b4a3. But a node is not necessarily an element, and an element must be a node.
What is node:

NODE is relative to the data structure of TREE. TREE is composed of NODE. You can refer to the tree diagram of discrete mathematics for this part.

What is element

ELEMENT is a concept in
XML, d55b48f42965904505cada53ebca2b59 is an element, which is one of the components of data in XML.
The difference between element (Element) and node (Node). Element is a small-scale definition. It must be a node containing complete information to be an element, such as e388a4556c0f65e1904146cc1a846bee...6fb279ad3fd4344cbdd93aac6ad173ac. But a node is not necessarily an element, and an element must be a node.

 

 
   
 
   
 

DOM regards everything in the document as node node>element

1DOM generates a tree according to the structure of the entire document when parsing the document, and saves all In memory

The advantage is that the entire document is always in memory, we can access any node at any time, and tree traversal is also a relatively familiar operation; the disadvantage is that it consumes memory and must wait until all documents are read. into memory for processing.
2 One thing to note is that the space between the two tags of the XML document is also a node (Text node) of this tree. 3499910bf9dac5ae3c52d5ede7383485 a4b561c25d9afb9ac8dc4d70affff4190d36329ec37a2cc24d42c7229b69747a 3499910bf9dac5ae3c52d5ede7383485 a has three nodes

Element root = doc.getDocumentElement();: What is root? ? ? ?
NodeList list = root.getChildNodes(); I don’t know whether root is a node or an element? ? ? ? ?

node has several subtypes:


    Element,
      Text,
    Attribute,
  RootElement,
    Comment,
    Namespace等

Element is a node that can have

attributes and child nodes.

Element is inherited

from Node

 //转换 if (node.getNodeType() == Element.ELEMENT_NODE)
{     Element e = (Element) node;  }

Does the element have children?

elemen et properties

1 e.getAttributes()

2 e.getChildNodes()


3 e.getTagName()


Element root = doc.getDocumentElement();: What is root? ? ? ?


NodeList list = root.getChildNodes(); I don’t know whether root is a node or an element? ? ?

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

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();
     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();
     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);
    }
   }
  }
 }

The above is the detailed content of Sample code that explains the difference between Node and Element in XML. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn