Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Dom Node Info in XML

Dom Node Info in XML

DOM Node Info in XML

In the XML DOM, every part of the XML document is represented as a node. Each node has properties that describe its type, name, value, and relationship to other nodes.


Key Node Properties

PropertyDescription
nodeTypeNumeric code representing the node type (see below)
nodeNameName of the node (e.g., tag name or #text for text nodes)
nodeValueValue of the node (text content for text nodes, null for elements)
parentNodeReference to the parent node
childNodesCollection of child nodes
firstChildFirst child node
lastChildLast child node
previousSiblingPrevious sibling node
nextSiblingNext sibling node

Common nodeType Values

Node TypenodeType ValueDescription
ELEMENT_NODE1An element (e.g., <book>)
ATTRIBUTE_NODE2An attribute (e.g., id="101")
TEXT_NODE3Text content inside elements
CDATA_SECTION_NODE4CDATA section
COMMENT_NODE8A comment node
DOCUMENT_NODE9The entire document

Example: Inspect Node Info

const book = xmlDoc.getElementsByTagName("book")[0];// Node typeconsole.log(book.nodeType);        // 1 (ELEMENT_NODE)// Node nameconsole.log(book.nodeName);        // "book"// Node value (for elements usually null)console.log(book.nodeValue);       // null// Access first child (e.g. <title>)const firstChild = book.firstChild;console.log(firstChild.nodeName);  // could be "#text" (whitespace) or "title"// To get the first element child ignoring whitespace:const firstElementChild = book.firstElementChild;console.log(firstElementChild.nodeName); // "title"// For a text node inside <title>const titleTextNode = firstElementChild.firstChild;console.log(titleTextNode.nodeType);  // 3 (TEXT_NODE)console.log(titleTextNode.nodeValue); // The text content of title

Use Case: Filtering Element Nodes Only

When looping over .childNodes, you often want to skip text or comment nodes:

for(let i = 0; i < book.childNodes.length; i++) {  const node = book.childNodes[i];  if(node.nodeType === 1) { // ELEMENT_NODE    console.log(node.nodeName); // logs element names only  }}

Summary

  • NodeType helps identify what kind of node you’re dealing with.

  • NodeName gives the name of element or #text for text nodes.

  • NodeValue holds text for text nodes, null for elements.

  • Use these to programmatically examine or manipulate XML content.


If you want, I can provide code examples for working with specific node types or handling mixed content!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql