Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7564003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:48:25+00:00 2026-05-30T13:48:25+00:00

I am trying to write a script that will walk through an HTML source

  • 0

I am trying to write a script that will walk through an HTML source and create a JSON file of the DOM that will then be shown in a tree view using d3.js. The problem I have is the desire to show not only the element (TITLE, P, LI, etc.) but also the value of the element. This is easy enough IF I only do it for end node elements (no children). But, I sometimes need it for parent objects as in the UL below for items II and B.

        <ul class="level-1">
            <li>I</li>
            <li>II
              <ul class="level-2">
                <li>A</li>
                <li>B
                  <ul class="level-3">
                    <li>1</li>
                    <li>2</li>
                    <li>3</li>
                  </ul>
                </li>
                <li>C</li>
              </ul>
            </li>
            <li>III</li>
        </ul>   

From my function below this line is working for end nodes but I cannot figure out how to do this for parents without printing everything from every child.

   $output.append(', "value": "' + $(child).text() + '"}');

I have tried some first-child stuff from jQuery but could not get it to work. I also want to keep this as generic as possible to feed in any html source. In other words, I do not want to say if (nodeName = ‘LI’) then -do list item specific stuff-

  var createJsonOutput = function(domObject) {

    var $currentChildren = domObject.children();
    var $childrenCnt = $currentChildren.length

      $.each($currentChildren, function(idx,child) {
         $output.append('{"name": "' + child.nodeName + '"');

         //does the child have children?
         if ($(child).children().length > 0) {
                $output.append(',"children": [');
                createJsonOutput($(child));
                $output.append(']}');
         } else if (child.nodeName != 'TEXTAREA' && child.nodeName != 'SCRIPT') {
                $output.append(', "value": "' + $(child).text() + '"}');
         } else {
                $output.append('}');
         }

         if ((idx + 1) < $childrenCnt) {
            $output.append(',');
         }
      });
  };

  createJsonOutput($('html'));

EXAMPLE (unformated) JSON:

{"name": "HTML","children": [{"name": "HEAD","children": [{"name": "META", "value": ""},{"name": "TITLE", "value": "Node-Link Tree"},{"name": "SCRIPT"},{"name": "SCRIPT"},{"name": "LINK", "value": ""}]},{"name": "BODY","children": [{"name": "DIV","children": [{"name": "UL","children": [{"name": "LI", "value": "I"},{"name": "LI","children": [{"name": "UL","children": [{"name": "LI", "value": "A"},{"name": "LI","children": [{"name": "UL","children": [{"name": "LI", "value": "1"},{"name": "LI", "value": "2"},{"name": "LI", "value": "3"}]}]},{"name": "LI", "value": "C"}]}]},{"name": "LI", "value": "III"}]}]},{"name": "DIV","children": [{"name": "TEXTAREA"},{"name": "P", "value": "tree time!"}]},{"name": "DIV", "value": ""},{"name": "SCRIPT"}]}]}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:48:26+00:00Added an answer on May 30, 2026 at 1:48 pm

    you can write a function to return just text of the current element,

    jQuery.fn.justtext = function() { 
        return $(this).clone()
                .children()
                .remove()
                .end()
                .text();
    };
    

    http://viralpatel.net/blogs/2011/02/jquery-get-text-element-without-child-element.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a script that will search through a html file and
I am trying to write a PHP script that will create an HTML form
I'm trying to write a script that will create a user in MediaWiki, so
I'm trying to write a simple ruby script that will copy a file to
I am trying to write a script that will look through a set of
I am trying to write a script that will parse a local file and
I'm trying to write a script that will download all the source material from
I'm trying to write a script that will download files from an FTP server.
I'm trying to write a bash script that will process a list of files
I'm trying to write a Perl script that will parse the output of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.