Io ho questo file DomDocumentParser.java:
codice:
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.parsers.*;
import java.io.File;
import java.io.IOException;
/**
* Parsing DOM di un file xml
*
*/
public class DomDocumentParser
{
public static org.w3c.dom.Node getChild(org.w3c.dom.Node parentNode,
int childIndex) {
org.w3c.dom.Node childNode = parentNode.getChildNodes().item(childIndex);
return childNode;
}
/** Array che mappa i tipi di elemento con l'indice int corrispondente
* alle specifiche per i node types di:
* Document Object Model (DOM) Level 2 Core Specification
* (http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113)
*/
static final String[] typeName = {
"none",
"Element",
"Attr",
"Text",
"CDATA",
"EntityRef",
"Entity",
"ProcInstr",
"Comment",
"Document",
"DocType",
"DocFragment",
"Notation"
};
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java DomDocumentParser filename");
System.exit(1);
}
DomDocumentParser ddp = new DomDocumentParser();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
/* Effettua il parsing del file xml passato come argomento ed istanzia
* l'oggetto Document su cui si effettuerà l'esplorazione dell'albero.
*/
Document document = builder.parse( new File(args[0]) );
ddp.printNodeInfo(document);
} catch (SAXException sxe) {
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
} // main
/** printNodeInfo(Node node)
* Metodo che esplora l'albero Dom ricorsivamente e stampa a video
* le informazioni sugli elementi.
*/
private void printNodeInfo(Node currentNode) {
String sNodeName = currentNode.getNodeName();
int iNodeType = currentNode.getNodeType();
String sNodeValue = currentNode.getNodeValue();
int iChildNumber = currentNode.getChildNodes().getLength();
NamedNodeMap nnmAttributes = currentNode.getAttributes();
System.out.print("\n");
System.out.println("Nome nodo: " + sNodeName);
System.out.println("Tipo nodo: " + typeName[iNodeType]);
System.out.println("Valore nodo: " + sNodeValue);
System.out.println("Numero figli: " + iChildNumber);
System.out.println("Attributi: " + printAttributes(nnmAttributes));
//Se non si tratta di una foglia continua l'esplorazione ricorsivamente
if (iChildNumber > 0) {
NodeList nlChilds = currentNode.getChildNodes();
for (int iChild = 0; iChild < iChildNumber; iChild++) {
printNodeInfo(nlChilds.item(iChild));
}
}
}
/** printAttributes(NamedNodeMap nnm)
* Metodo utilizzato per la formattazione degli attributi ricavati da
* un elemento.
*/
private static String printAttributes(NamedNodeMap nnm) {
String sAttrList = new String();
if (nnm != null && nnm.getLength() > 0) {
for (int iAttr=0; iAttr < nnm.getLength(); iAttr++) {
sAttrList += nnm.item(iAttr).getNodeName();
sAttrList += "=";
sAttrList += nnm.item(iAttr).getNodeValue();
sAttrList += "; ";
}
return sAttrList;
}
else {
return "assenti";
}
}
}
e riesco a creare il file DomDocumentParser.class
Solo che quando devo fare partire con
java DomDocumentPaser file.xml
mi da quest'eccezione:
Exception in thread "main" java.lang.NoClassDefFoundError: DomDocumentParser
Sapreste darmi questa spiegazione?
Grazie!