title: Get tags array from HTML
body: <p>I need to get the tags from HTML to process the text it to draw text in Canvas, now I'm getting the tree like this:</p>
  
  <pre><code>    private JSONArray printTree (NodeList nodes, int level, JSONArray data) throws JSONException {
  
          for (int i = 0; i &lt; nodes.getLength (); i++) {
  
              Node node = nodes.item (i);
  
              JSONObject data2 = new JSONObject ();
  
              if (node.getNodeType () != Node.TEXT_NODE) {
  
                  data2.put ("tag", node.getNodeName ());
                  data2.put ("body", printTree (node.getChildNodes (), level, new JSONArray ()));
  
              } else data2.put ("text", node.getNodeValue ());
  
              data.put (data2);
  
          }
  
          return data;
  
      }
  </code></pre>
  
  <p>so this html</p>
  
  <pre><code>&lt;p&gt;
    &lt;b&gt;
      &lt;i&gt;
        text1
      &lt;/i&gt;
      text2
    &lt;/b&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>returns the following array:</p>
  
  <pre><code>[
    {
      "tag": "p",
      "body": [
        {
          "tag": "b",
          "body": [
            {
              "tag": "i",
              "body": [
                {
                  "text": "text1",
                }
              ]
            },
            {
              "text": "text2",
            }
          ]
        }
      ]
    }
  ]
  </code></pre>
  
  <p>But how can I get the tags associated with text, according its nested level, e. g., because I need to process every text in tags with this tags?</p>
  
  <pre><code>[
    {
      "text": "text1",
      "tags": ["p","b","i"]
    },
    {
      "text": "text2",
      "tags": ["p","b"]
    }
  ]
  </code></pre>