DOM Question #6 – Serialization

Write code to serialize given HTML

Serialization means converting given HTML to JSON format. In the JSON we need to store node name, node type, children, props.

<div id=”foo”>
<h1>Hello</h1>
<p class=”bar”>
<span&…


This content originally appeared on DEV Community and was authored by Shweta Kale

Write code to serialize given HTML

Serialization means converting given HTML to JSON format. In the JSON we need to store node name, node type, children, props.

<div id="foo">
  <h1>Hello</h1>
  <p class="bar">
    <span>World!</span>
  </p>
</div>

We will create a function and call it recursively on all HTML children to get JSON data in required format.

function getNodeProps(node){
  let props = {};
  for(let i=0; i< node.attributes.length; i++){
    const attribute = node.attributes[i];
    props[attribute.nodeName] = attribute.nodeValue;
  }
  return props;
}

function serialize(node){
  const serializedJSON = {};
  serializedJSON.nodeName = node.nodeName;
  serializedJSON.nodeType = node.nodeType;

  const props = getNodeProps(node);
  serializedJSON.props = props;

  let children = [];
  if(node.children.length>0){
   for(let i=0; i< node.children.length; i++){
    children.push(serialize(node.children[i]));
   }}
  serializedJSON.children = children;

  return serializedJSON;
}

This will give us serialized JSON but what about time complexity?

Total time complexity: Since the function recursively visits every node in the tree, the total time complexity is proportional to the number of nodes and the number of attributes and children. In the worst case, the time complexity is:

O(N×(A+C))

Where:

  • N: is the total number of nodes.
  • A: is the average number of attributes per node.
  • C: is the average number of children per node.


This content originally appeared on DEV Community and was authored by Shweta Kale


Print Share Comment Cite Upload Translate Updates
APA

Shweta Kale | Sciencx (2025-02-28T04:36:04+00:00) DOM Question #6 – Serialization. Retrieved from https://www.scien.cx/2025/02/28/dom-question-6-serialization/

MLA
" » DOM Question #6 – Serialization." Shweta Kale | Sciencx - Friday February 28, 2025, https://www.scien.cx/2025/02/28/dom-question-6-serialization/
HARVARD
Shweta Kale | Sciencx Friday February 28, 2025 » DOM Question #6 – Serialization., viewed ,<https://www.scien.cx/2025/02/28/dom-question-6-serialization/>
VANCOUVER
Shweta Kale | Sciencx - » DOM Question #6 – Serialization. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/28/dom-question-6-serialization/
CHICAGO
" » DOM Question #6 – Serialization." Shweta Kale | Sciencx - Accessed . https://www.scien.cx/2025/02/28/dom-question-6-serialization/
IEEE
" » DOM Question #6 – Serialization." Shweta Kale | Sciencx [Online]. Available: https://www.scien.cx/2025/02/28/dom-question-6-serialization/. [Accessed: ]
rf:citation
» DOM Question #6 – Serialization | Shweta Kale | Sciencx | https://www.scien.cx/2025/02/28/dom-question-6-serialization/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.