CollectionsCollections Database

Parser XML

Il parser XML permette di parsare ed attraversare un documento XML da Javascript.

Puoi ottenere il documento XML dalla risposta di una richiesta tramite response.xml(). In alternativa, puoi anche fare il parsing di una string con app.xml.parse(string).

response.xml() XML document
Restituisce un documento XML dalla risposta di una richiesta. Restituisce undefined se la risposta non può essere parsata.
let request = app.request("https://www.google.com");
let response = request.send();
let xml_document = response.xml();
app.xml.parse(string string) XML document
Crea un documento XML da una stringa. Restituisce undefined se la stringa non può essere parsata.
let xml_string = '<?xml version="1.0"?><book>Harry Potter</book>';
let xml_document = app.xml.parse(xml_string);

XML Document

xml.document (subclass of xml.node)
Questa classe gestisce un documento XML. È una sottoclasse di XML Node..
root() XML element
Restituisce l'elemento alla radice del documento.

XML Node

xml.node
Questa classe gerstisce un nodo XML generico.
find(expression string) array of nodes
Restituisce dei nodi applicando un'espressione simile ad XPath a questo nodo. L'espressione è limitata ai selettori di discendenza, altri selettori non sono al momento supportati.

Esempio 1: document.find("/books/book/author");
Trova author seguendo il percorso partendo dalla radice.

Esempio 2: book.find("./author");
Trova author nel nodo corrente.

Esempio 3: document.find("//author");
Trova author ovunque.
type() string
Restituisce il tipo di nodo.
Node type: "document", "element", "text", "comment"
string() string
Restituisce il contenuto del nodo come stringa.
child(index number) XML node
Ottiene il figlio del nodo all'indice specificato.
childCount() number
Ottiene il numero di figli del nodo.
prevNode() XML node
Restituisce il nodo precedente attraversando la struttura ad albero.
prevSibling() XML node
Restituisce il fratello precedente (nodo avente lo stesso padre del nodo corrente).
nextNode() XML node
Restituisce il nodo successivo attraversando la struttura ad albero.
nextSibling() XML node
Restituisce il fratello successivo (nodo avente lo stesso padre del nodo corrente).
parent() XML node
Restituisce il nodo padre.

XML Element

xml.element (subclass of xml.node)
Questa classe gestisce un elemento XML. È una sottoclasse di XML Node.
getAttribute(name string) string
Ottiene il valore dell'attributo dal suo nome.

Codice di esempio

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book>
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
    </book>
    <book>
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
    </book>
</bookstore>
let request = app.request("https://www.google.com");
let response = request.send();
if (!response.success) {
    app.fail();
}

let xml_document = response.xml();
let books = xml_document.find("/bookstore/book");

for (let book of books) {
    // Get title node
    let titleNode = book.find("./title")[0];
    // Get title
    let title = titleNode.string();
    // Get language attribute
    let language = titleNode.getAttribute("lang");
    // Get author
    let author = book.find("./author")[0].string();
}