08_XML

annuncio pubblicitario
Cenni su “XML in Java”
Docente: Gabriele Lombardi
[email protected]
© 2010 - CEFRIEL
The present original document was produced by CEFRIEL and the Teacher for the benefit and internal use of this
course, and nobody else may claim any right or paternity on it. No right to use the document for any purpose other than
the Intended purpose and no right to distribute, disclose, release, furnish or disseminate it or a part of it in any way or
form to anyone without the prior express written consent of CEFRIEL and the Teacher.
© copyright Cefriel and the Teacher-Milan-Italy-23/06/2008. All rights reserved in accordance with rule of law and
international agreements.
© 2010 - CEFRIEL
Sommario
SLIDE
CONTENUTO
XML
Basi relative all’XML.
Streaming
Leggere e scrivere XML.
XMLEnc/Dec, JAXB
Persistenza tramite XML.
Validazione
Validare il contenuto di un documento.
JAXP
Trasformatori di XML come framework generico.
Java e XML…
Riferimenti a un mondo da scoprire!
© 2010 - CEFRIEL
XML: eXtendible Markup Language

eXtendible:
– l’XML è un meta-linguaggio, è quindi possibile
definirne istanze proprietarie che lo “estendano”;
– nuovi tag e nuovi attributi possono essere definiti,
assieme alle regole grammaticali per utilizzarli.
– esempio: XHTML e RSS sono due diverse
applicazioni XML, con scopi e gramatiche differenti.

Markup:
– “come per le bustine da congelatore”, l’XML ha lo
scopo di contenere e conservare… dati etichettati;
– i tag sono i contenitori, gli attributi sono le etichette.

Language:
– sarebbe più corretto meta-language, solo la sintassi è
definita, la grammatica dei tag è definita nelle istanze.
© 2010 - CEFRIEL
XML: basi di sintassi

Un documento XML è:
– un albero radicato in un nodo di tipo Document;
– formato differenti tipologie di nodi;
– la radice deve contenere uno e un solo sotto-nodo di tipo di tipo
Element (spesso chiamati tag);
– case-sensitive e normalmente indipendente dall’indentazione
(eccetto i nodi di testo);
– ma soprattutto… è testo “umanamente” leggibile!

Un esempio:
Direttiva
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vcards>
<vcard surname="Lombardi" name="Gabriele">
<addresses>
<address>[email protected]</address>
<address>[email protected]</address>
</addresses>
<telephone>
<number>0234567890</number>
<number>+39340340340</number>
</telephone>
</vcard>
<vcard surname="test" name="test"/>
</vcards>
© 2010 - CEFRIEL
Root element
Element con attributi
Element connsotto-element
Element con testo
Chiusura di un element
Element vuoto (con attributi)
XML: basi di sintassi



<? direttiva ?>
<!-- Commento su riga multipla -->
&nome; si chiama entity, genera di solito un carattere
speciale come ‘<’ o ‘&’:
– <  < >  >
&  &
– à  à
&quote;  “
… vedere specification.




<!istruzione…>
<![CDATA[dati verbatim (senza interpretazione caratteri
speciali, ad es. per blocchi di codice)]]>
<elementVuoto attributo=“valore”/>
<elementAnnidati>
<spazioNomi:el2
xmlns:spazioNomi=“ID spazio dei nomi”/>
<el3 xmlns=“spazio dei nomi di default”>
<el4/>
</el3>
</elementAnnidati>
© 2010 - CEFRIEL
XML in Java: streaming

Scopo:
– leggere e scrivere documenti XML come flusso di
nodi dell’albero rappresentato dal documento;
– evitare di caricare in memoria un intero documento;
– generare/ricevere eventi verso/dal documento
(di creazione/rilevamento di nodi).

Strumenti (javax.xml.stream.*):
– XMLOutputFactory:
• generazione di stream XML in uscita;
– XMLInputFactory:

• generazione di stream XML in ingresso.
Esempio:
– Code\08_XML\XmlIO\...\Streaming.java
© 2010 - CEFRIEL
XML in Java: encoding/decoding

Scopo:
– codifica e decodifica di dati in XML (solo per java);
– persistenza ottenibile senza infrastrutture ausiliarie;
– come java.io.Object*Stream… ma in XML.

Strumenti: (java.beans.*):
– XMLEncoder:
• codifica di oggetti in XML;
– XMLDecoder:

• decodifica di oggetti in XML.
Esempio:
– Code\08_XML\XmlIO\...\EncDec.java
© 2010 - CEFRIEL
XML in Java: JAXB

Scopo:
– mappare oggetti POJO (anche strutture dati
articolate) in documenti XML (con validazione);
– riutilizzare classi già esistenti;
– sintassi dichiarativa tramite annotazioni.

Strumenti: (javax.xml.bind.*):
– JAXBContext:

• contesto di .marshalling/unmarshalling;
• conosce classi e mapping co l’XML;
• ci fornisce Marshaller e Unmarshaller.
Esempio:
– Code\08_XML\XmlIO\...\JAXB.java
© 2010 - CEFRIEL
Scarica