---
title: Parser HTML e XML
project: Total.js V5
version: 0.0.20
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v5/0-0-20/reference/html-parser/
updated: 2026-11-19T16:17:02.153Z
---

# Parser HTML e XML

> String.prototype.parseHTML(), HTMLElement e i metodi di un elemento.

## API

### String.prototype.parseHTML([trim])

Type: `method`

Analizza l'HTML in un albero di elementi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `trim` | `Boolean` | no | `false` | Ripulisce dagli spazi i nodi di testo. |

Returns: `HTMLElement` L'elemento radice.

Restituisce l'elemento radice; i suoi `children` sono i nodi di primo livello. I commenti vengono rimossi. `<script>`, `<style>` e `<template>` mantengono il loro contenuto come testo. Con `trim: true` i nodi di testo vengono ripuliti dagli spazi.

`String.prototype.parseXML2()` analizza l'XML allo stesso modo: ogni elemento mantiene il suo prefisso (`soap:Body`) e il testo degli elementi viene unito con a capo da `text()`.

Example: Leggere i link
```js
var dom = html.parseHTML();
var links = dom.find('a').map(function(a) {
	return { url: a.attr('href'), text: a.text() };
});
```

Example: XML
```js
var xml = body.parseXML2();
var items = xml.find('*:item'); // any prefix
```

### HTMLElement

Type: `class`

Un elemento (o un nodo di testo) di un albero di `parseHTML()`.

| Proprietà | Descrizione |
| --- | --- |
| `tagName` | Nome del tag in maiuscolo, `TEXT` per un nodo di testo |
| `raw` | Nome del tag come è scritto |
| `prefix` | Prefisso XML con i due punti, come `SOAP:` |
| `attrs` | Attributi come oggetto |
| `children` | Nodi figli |
| `parentNode` | Elemento genitore |
| `textContent` | Testo di un nodo `TEXT` |
| `innerHTML`, `outerHTML`, `innerText` | Getter per `html()`, l'elemento con il suo tag, e `text()` |

Example: Attraversare
```js
var dom = '<ul><li>A</li><li>B</li></ul>'.parseHTML();
var ul = dom.children[0];
ul.tagName;		   // 'UL'
ul.children.length;   // 2
ul.outerHTML;		 // '<ul><li>A</li><li>B</li></ul>'
```

### element.find(selector)

Type: `method`

Trova gli elementi sotto questo elemento.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | Selettore simile a CSS. |

Returns: `Array` Elementi trovati.

Supporta nomi di tag, `*`, `#id`, `.class`, `[attr="value"]`, `[attr]` (l'elemento ha l'attributo), `prefix:` e `*:name` per XML, discendenti (`div p`), figli (`div > p`, o `> p` per i figli diretti di questo elemento) e più selettori separati da virgole.

Il risultato è un array con i metodi `aclass()`, `rclass()`, `tclass()`, `attr()`, `attrd()`, `find()`, `html()` (uguale a `toString()`) e `text()`, applicati a tutti gli elementi trovati.

Example: Trovare
```js
dom.find('table.data > tr td').text();
dom.find('a').attr('rel', 'nofollow');
```

Example: Elementi con un attributo
```js
var dom = html.parseHTML();
var images = dom.find('img[alt]');      // images with an alt text
var ids = dom.find('[data-id]');
```

### element.parent()

Type: `method`

Restituisce l'elemento genitore.

Returns: `HTMLElement` Il genitore.

Uguale a `element.parentNode`.

Example: Genitore
```js
var td = dom.find('td.price')[0];
var row = td.parent();
```

### element.browse(fn, [reverse])

Type: `method`

Chiama una funzione per ogni nodo sotto questo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(node)`; `true` salta i figli. |
| `reverse` | `Boolean` | no | `false` | Attraversa i genitori. |

Returns: `HTMLElement` Questo elemento.

`fn(node)` viene chiamata per ogni nodo sottostante, nodi di testo (`tagName` `TEXT`) inclusi, in profondità. Restituisci `true` per saltare i figli di quell'elemento. Con `reverse: true` risale invece attraverso i genitori.

Example: Raccogliere
```js
var tags = {};
dom.browse(function(el) {
	tags[el.tagName] = (tags[el.tagName] || 0) + 1;
});
```

### element.attr(name, [value])

Type: `method`

Legge o imposta un attributo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Nome dell'attributo. |
| `value` | `Object` | no |  | Nuovo valore. |

Returns: `String` Il valore in lettura, altrimenti l'elemento.

Senza `value` restituisce l'attributo. Un valore vuoto o `null` lo rimuove; gli altri valori vengono salvati come stringhe.

Example: Attributi
```js
var href = a.attr('href');
a.attr('rel', 'nofollow');
a.attr('target', null);
```

### element.attrd(name, [value])

Type: `method`

Legge o imposta un attributo `data-`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Nome senza `data-`. |
| `value` | `Object` | no |  | Nuovo valore. |

Returns: `String` Il valore in lettura, altrimenti l'elemento.

`attrd('id')` equivale a `attr('data-id')`.

Example: Dati
```js
el.attrd('id', 123);
el.attrd('id'); // '123'
```

### element.aclass(cls)

Type: `method`

Aggiunge classi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Nomi delle classi. |

Returns: `HTMLElement` Questo elemento.

Più classi sono separate da spazi o virgole.

Example: Aggiungere
```js
el.aclass('active selected');
```

### element.rclass(cls)

Type: `method`

Rimuove classi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Nomi delle classi. |

Returns: `HTMLElement` Questo elemento.

Più classi sono separate da spazi o virgole.

Example: Rimuovere
```js
el.rclass('hidden');
```

### element.tclass(cls, [value])

Type: `method`

Alterna classi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Nomi delle classi. |
| `value` | `Boolean` | no |  | Forza l'aggiunta o la rimozione. |

Returns: `HTMLElement` Questo elemento.

Senza `value` ogni classe viene alternata; `true` aggiunge e `false` rimuove.

Example: Alternare
```js
el.tclass('active', item.id === current);
```

### element.hclass(cls)

Type: `method`

Verifica una classe.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cls` | `String` | yes |  | Un nome di classe. |

Returns: `Boolean`

Restituisce `true` quando l'elemento ha la classe.

Example: Verificare
```js
if (el.hclass('price'))
	console.log(el.text());
```

### element.css(key, [value])

Type: `method`

Imposta gli stili inline.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String|Object` | yes |  | Proprietà o oggetto. |
| `value` | `String` | no |  | Valore; vuoto rimuove. |

Returns: `HTMLElement` Questo elemento.

Accetta una proprietà e un valore, o un oggetto di proprietà. Un valore vuoto rimuove la proprietà dall'attributo `style`.

Example: Stili
```js
el.css('color', 'red');
el.css({ width: '100px', display: '' });
```

### element.remove()

Type: `method`

Rimuove l'elemento dal suo genitore.

Returns: `HTMLElement` L'elemento rimosso.

Example: Rimuovere gli script
```js
dom.find('script').forEach(function(el) {
	el.remove();
});
```

### element.append(html)

Type: `method`

Analizza l'HTML e lo aggiunge alla fine dei figli.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | HTML da aggiungere. |

Returns: `HTMLElement` Il nuovo elemento o i nuovi elementi.

Restituisce il nuovo elemento, oppure un array quando l'HTML ha più nodi di primo livello.

Example: Accodare
```js
ul.append('<li>New</li>');
```

### element.prepend(html)

Type: `method`

Analizza l'HTML e lo aggiunge all'inizio dei figli.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | HTML da aggiungere. |

Returns: `HTMLElement` La radice analizzata.

Restituisce la radice analizzata del nuovo HTML.

Example: Anteporre
```js
body.prepend('<p>Notice</p>');
```

### element.text()

Type: `method`

Restituisce il testo dell'elemento.

Returns: `String` Testo.

I nodi di testo vengono uniti e decodificati; il testo degli elementi di blocco (`div`, `p`, titoli, elenchi, tabelle...) è separato da a capo.

Example: Testo
```js
'<p>Hello <b>world</b></p>'.parseHTML().text(); // 'Hello world'
```

### element.html([formatted])

Type: `method`

Restituisce l'HTML interno.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `formatted` | `Boolean` | no | `false` | Indenta l'output. |

Returns: `String` HTML.

`formatted: true` mette ogni nodo su una nuova riga con tabulazioni. `toString(formatted, outer)` fa lo stesso e con `outer: true` include l'elemento stesso.

Example: Serializzare
```js
var html = dom.html();
var pretty = dom.html(true);
```
